 # Creating a new colormap based on a cmap from matplotlib.cm?

Hi all,

Does anyone know of a good way to create a new LinearSegmentedColormap based off an existing one?

I have a function which attempts to generate N “optimal” color map indices for a given data array. In cases where the number of values specified in _segmentdata is the same as N, then I can simply copy _segmentdata and modify the indices and create a new color map. In many cases, however, the segment data has far fewer interpolation points, e.g.:

In : cm.gray._segmentdata

Out:

{‘blue’: ((0.0, 0, 0), (1.0, 1, 1)),

‘green’: ((0.0, 0, 0), (1.0, 1, 1)),

‘red’: ((0.0, 0, 0), (1.0, 1, 1))}

Other colormaps may have an arbitrary number of interpolation points. Ideally, what I would like is a way to expand this into N points (e.g. 256) so that I use that as input for my new map.

Any suggestions? I found a similar post from last year, but that doesn’t seem to be applicable in the version of Matplotlib I’m using (1.1.0).

Thanks,
Keith

+------------------------------------------------ Keith Hughitt ---------+

Hi all,

Does anyone know of a good way to create a new LinearSegmentedColormap
based off an existing one?

I have a function which attempts to generate N "optimal" color map
indices for a given data array. In cases where the number of values
specified in _segmentdata is the same as N, then I can simply
copy _segmentdata and modify the indices and create a new color map. In
many cases, however, the segment data has far fewer interpolation
points, e.g.:

In : cm.gray._segmentdata
Out:
{'blue': ((0.0, 0, 0), (1.0, 1, 1)),
'green': ((0.0, 0, 0), (1.0, 1, 1)),
'red': ((0.0, 0, 0), (1.0, 1, 1))}

Other colormaps may have an arbitrary number of interpolation points.
Ideally, what I would like is a way to expand this into N points (e.g.
256) so that I use that as input for my new map.

so elegant to expand cmaps.

did you try to understand the way cmaps can be defined by gradient
borders? -> http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps

i.e. a friend gave me a colormap defined by points and i converted it to
something like that (by just looking at the colors):

ie_data = {
'red': ((0.00, 0.357, 0.357),
(0.50, 1.000, 1.000),
(0.75, 1.000, 1.000),
(1.00, 1.000, 1.000)),
'green':((0.00, 0.467, 0.467),
(0.50, 1.000, 1.000),
(0.75, 0.750, 0.750),
(1.00, 0.371, 0.371)),
'blue':((0.00, 0.800, 0.800),
(0.50, 1.000, 1.000),
(0.75, 0.157, 0.157),
(1.00, 0.157, 0.157))
}
ie = matplotlib.colors.LinearSegmentedColormap('ieMap', ie_data, 256)

use it with cmap=ie

regards