[yt-svn] commit/yt: xarthisius: Merged in samskillman/yt (pull request #1189)
commits-noreply at bitbucket.org
commits-noreply at bitbucket.org
Mon Sep 8 19:18:50 PDT 2014
1 new commit in yt:
https://bitbucket.org/yt_analysis/yt/commits/4444ee578e99/
Changeset: 4444ee578e99
Branch: yt
User: xarthisius
Date: 2014-09-09 04:18:42
Summary: Merged in samskillman/yt (pull request #1189)
Add colorbrewer2 colormaps via (optional) brewer2mpl
Affected #: 4 files
diff -r 793de33241a8059c099cbaade4ec7ed0f1e1741f -r 4444ee578e9968f136123f87125e02d41c84737e doc/source/visualizing/colormaps/index.rst
--- a/doc/source/visualizing/colormaps/index.rst
+++ b/doc/source/visualizing/colormaps/index.rst
@@ -6,12 +6,17 @@
There are several colormaps available for yt. yt includes all of the
matplotlib colormaps as well for nearly all functions. Individual visualization
functions usually allow you to specify a colormap with the ``cmap`` flag.
-There are a small number of functions (mostly contained in the image_writer
-module; e.g. write_bitmap, write_image, write_projection, etc.), which do
-not load the matplotlib infrastructure and can only access the colormaps
-native to yt.
-Here is a chart of all of the colormaps available. In addition to each
+If you have installed `brewer2mpl`
+(`pip install brewer2mpl` or see `https://github.com/jiffyclub/brewer2mpl`_),
+you can also access the discrete colormaps available on
+`http://colorbrewer2.org`_. Instead of supplying the colormap name, specify
+a tuple of the form (name, type, number), for example `('RdBu', 'Diverging', 9)`.
+These discrete colormaps will not be interpolated, and can be useful for
+creating colorblind/printer/grayscale-friendly plots. For more information,
+visit `http://colorbrewer2.org`_.
+
+Here is a chart of all of the yt and matplotlib colormaps available. In addition to each
colormap displayed here, you can access its "reverse" by simply appending a
``"_r"`` to the end of the colormap name.
diff -r 793de33241a8059c099cbaade4ec7ed0f1e1741f -r 4444ee578e9968f136123f87125e02d41c84737e yt/visualization/base_plot_types.py
--- a/yt/visualization/base_plot_types.py
+++ b/yt/visualization/base_plot_types.py
@@ -19,6 +19,12 @@
from yt.funcs import \
get_image_suffix, mylog, iterable
import numpy as np
+try:
+ import brewer2mpl
+ has_brewer = True
+except:
+ has_brewer = False
+
class CallbackWrapper(object):
def __init__(self, viewer, window_plot, frb, field):
@@ -110,6 +116,13 @@
elif (cbnorm == 'linear'):
norm = matplotlib.colors.Normalize()
extent = [float(e) for e in extent]
+ if isinstance(cmap, tuple):
+ if has_brewer:
+ bmap = brewer2mpl.get_map(*cmap)
+ cmap = bmap.get_mpl_colormap(N=cmap[2])
+ else:
+ raise RuntimeError("Please install brewer2mpl to use colorbrewer colormaps")
+
self.image = self.axes.imshow(data.to_ndarray(), origin='lower',
extent=extent, norm=norm, vmin=self.zmin,
aspect=aspect, vmax=self.zmax, cmap=cmap)
diff -r 793de33241a8059c099cbaade4ec7ed0f1e1741f -r 4444ee578e9968f136123f87125e02d41c84737e yt/visualization/image_writer.py
--- a/yt/visualization/image_writer.py
+++ b/yt/visualization/image_writer.py
@@ -23,6 +23,12 @@
import yt.utilities.lib.image_utilities as au
import yt.utilities.png_writer as pw
from yt.extern.six.moves import builtins
+try:
+ import brewer2mpl
+ has_brewer = True
+except:
+ has_brewer = False
+
def scale_image(image, mi=None, ma=None):
r"""Scale an image ([NxNxM] where M = 1-4) to be uint8 and values scaled
@@ -248,7 +254,14 @@
lut = cmd.color_map_luts[cmap_name]
except KeyError:
try:
- cmap = mcm.get_cmap(cmap_name)
+ if isinstance(cmap_name, tuple):
+ if has_brewer:
+ bmap = brewer2mpl.get_map(*cmap_name)
+ cmap = bmap.get_mpl_colormap(N=cmap_name[2])
+ else:
+ raise RuntimeError("Please install brewer2mpl to use colorbrewer colormaps")
+ else:
+ cmap = mcm.get_cmap(cmap_name)
dummy = cmap(0.0)
lut = cmap._lut.T
except ValueError:
@@ -256,10 +269,19 @@
" colormap file or matplotlib colormaps"
raise KeyError(cmap_name)
- x = np.mgrid[0.0:1.0:lut[0].shape[0]*1j]
- shape = buff.shape
- mapped = np.dstack(
- [(np.interp(buff, x, v)*255) for v in lut ]).astype("uint8")
+ if isinstance(cmap_name, tuple) and has_brewer:
+ # If we are using the colorbrewer maps, don't interpolate
+ shape = buff.shape
+ # We add float_eps so that digitize doesn't go out of bounds
+ x = np.mgrid[0.0:1.0+np.finfo(np.float32).eps:lut[0].shape[0]*1j]
+ inds = np.digitize(buff.ravel(), x)
+ inds.shape = (shape[0], shape[1])
+ mapped = np.dstack([(v[inds]*255).astype('uint8') for v in lut])
+ del inds
+ else:
+ x = np.mgrid[0.0:1.0:lut[0].shape[0]*1j]
+ mapped = np.dstack(
+ [(np.interp(buff, x, v)*255).astype('uint8') for v in lut ])
return mapped.copy("C")
def strip_colormap_data(fn = "color_map_data.py",
diff -r 793de33241a8059c099cbaade4ec7ed0f1e1741f -r 4444ee578e9968f136123f87125e02d41c84737e yt/visualization/plot_container.py
--- a/yt/visualization/plot_container.py
+++ b/yt/visualization/plot_container.py
@@ -35,6 +35,7 @@
from yt.utilities.exceptions import \
YTNotInsideNotebook
+
def invalidate_data(f):
@wraps(f)
def newfunc(*args, **kwargs):
@@ -206,8 +207,11 @@
field : string
the field to set the colormap
if field == 'all', applies to all plots.
- cmap_name : string
- name of the colormap
+ cmap_name : string or tuple
+ If a string, will be interpreted as name of the colormap.
+ If a tuple, it is assumed to be of the form (name, type, number)
+ to be used for brewer2mpl functionality. (name, type, number, bool)
+ can be used to specify if a reverse colormap is to be used.
"""
@@ -384,37 +388,6 @@
return self.set_font({'size': size})
@invalidate_plot
- def set_cmap(self, field, cmap):
- """set the colormap for one of the fields
-
- Parameters
- ----------
- field : string
- the field to set a transform
- if field == 'all', applies to all plots.
- cmap : string
- name of the colormap
-
- """
- if field == 'all':
- fields = self.plots.keys()
- else:
- fields = [field]
-
- for field in self.data_source._determine_fields(fields):
- self._colorbar_valid = False
- self._colormaps[field] = cmap
- if isinstance(cmap, types.StringTypes):
- if str(cmap) in yt_colormaps:
- cmap = yt_colormaps[str(cmap)]
- elif hasattr(matplotlib.cm, cmap):
- cmap = getattr(matplotlib.cm, cmap)
- if not is_colormap(cmap) and cmap is not None:
- raise RuntimeError("Colormap '%s' does not exist!" % str(cmap))
- self.plots[field].image.set_cmap(cmap)
- return self
-
- @invalidate_plot
@invalidate_figure
def set_figure_size(self, size):
"""Sets a new figure size for the plot
Repository URL: https://bitbucket.org/yt_analysis/yt/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
More information about the yt-svn
mailing list