Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ENH: Add LinearColormap class.

  • Loading branch information...
commit 283201e436a88a9cf7a93d9a7bfbc8c5b0142b57 1 parent 80b3295
@tonysyu authored
Showing with 103 additions and 1 deletion.
  1. +48 −0 doc/examples/color/plot_linear_colormap.py
  2. +55 −1 mpltools/color.py
View
48 doc/examples/color/plot_linear_colormap.py
@@ -0,0 +1,48 @@
+"""
+==============
+LinearColormap
+==============
+
+This class simplifies the creation of Matplotlib. To specify a colormap, you
+can just specify key colors in the colormap, and ``LinearColormap`` will
+distribute those colors evenly in the colormap and linearly interpolate
+in-between. In the example below, specifying two colors defines the minimum and
+maximum color values of the colormap.
+"""
+import numpy as np
+import matplotlib.pyplot as plt
+
+from mpltools import color
+
+
+x, y, z = np.random.uniform(size=(3, 100))
+
+white_red = color.LinearColormap('white_red', {'red': [1, 0.8],
+ 'green': [1, 0],
+ 'blue': [1, 0]})
+plt.scatter(x, y, c=z, cmap=white_red, s=200)
+
+"""
+.. image:: PLOT2RST.current_figure
+
+To get more complicated, use the ``index`` argument to specify where the color
+values map to in the colormap. Here, we repeat an index to get a segmented
+colormap. This colormap is uniformly blue below the midpoint and red above the
+midpoint. Alpha values are maximum at the edges and minimum in the middle.
+"""
+
+bcr_spec = {'blue': [0.4, 0.4, 0.1, 0.1],
+ 'green': [0.2, 0.2, 0.0, 0.0],
+ 'red': [0.02, 0.02, 0.4, 0.4],
+ 'alpha': [1, 0.3, 0.3, 1]}
+blue_clear_red = color.LinearColormap('blue_clear_red', bcr_spec,
+ index=[0, 0.5, 0.5, 1])
+
+
+plt.figure()
+plt.scatter(x, y, c=z, cmap=blue_clear_red, s=200, edgecolors='none')
+
+"""
+.. image:: PLOT2RST.current_figure
+"""
+plt.show()
View
56 mpltools/color.py
@@ -1,10 +1,64 @@
import numpy as np
import matplotlib.pyplot as plt
+from matplotlib.colors import LinearSegmentedColormap
from ._config import config
-__all__ = ['color_mapper', 'colors_from_cmap', 'cycle_cmap']
+__all__ = ['color_mapper', 'colors_from_cmap', 'cycle_cmap', 'LinearColormap']
+
+
+class LinearColormap(LinearSegmentedColormap):
+ """Create Matplotlib colormap with color values specified at key points.
+
+ This class simplifies the call signature of LinearSegmentedColormap. By
+ default, colors specified by `segmented_data` are equally spaced along the
+ colormap.
+
+ Parameters
+ ----------
+ name : str
+ Name of colormap.
+ segmented_data : dict
+ Dictionary of 'red', 'green', 'blue', and (optionally) 'alpha' values.
+ Each color key contains a list of `x`, `y` tuples. `x` must increase
+ monotonically from 0 to 1 and corresponds to input values for a mappable
+ object (e.g. an image). `y` corresponds to the color intensity.
+ index : list of floats (0, 1)
+ Note that these indices must match the length of `segmented_data`.
+ If None, colors in `segmented_data` are equally spaced in colormap.
+
+ Examples
+ --------
+ Linear colormap going from white to red
+ >>> white_red = LinearColormap('white_red', {'blue': [1.0, 0.0],
+ 'green': [1.0, 0.0],
+ 'red': [1.0, 0.8]})
+
+ Colormap going from blue to white to red
+ >>> bwr = LinearColormap('white_red',
+ {'blue': [0.4, 1.0, 0.1],
+ 'green': [0.2, 1.0, 0.0],
+ 'red': [0.0, 1.0, 0.4]})
+
+ You can use a repeated index to get a segmented color.
+ - Blue below midpoint of colormap, red above mid point.
+ - Alpha maximum at the edges, minimum in the middle.
+ >>> bcr_spec = {'blue': [0.4, 0.4, 0.1, 0.1],
+ 'green': [0.2, 0.2, 0.0, 0.0],
+ 'red': [0.02, 0.02, 0.4, 0.4],
+ 'alpha': [1, 0.3, 0.3, 1]}
+ >>> blue_clear_red = LinearColormap('blue_clear_red', bcr_spec,
+ index=[0, 0.5, 0.5, 1])
+ """
+
+ def __init__(self, name, segmented_data, index=None, **kwargs):
+ if index is None:
+ # If index not given, RGB colors are evenly-spaced in colormap.
+ index = np.linspace(0, 1, len(segmented_data['red']))
+ segmented_data = dict((key, [(x, y, y) for x, y in zip(index, value)])
+ for key, value in segmented_data.iteritems())
+ LinearSegmentedColormap.__init__(self, name, segmented_data, **kwargs)
CMAP_RANGE = config['color']['cmap_range']
Please sign in to comment.
Something went wrong with that request. Please try again.