Skip to content

Commit d26f36f

Browse files
committed
Merged revisions 8933 via svnmerge from
https://matplotlib.svn.sourceforge.net/svnroot/matplotlib/branches/v1_0_maint ........ r8933 | weathergod | 2011-01-22 10:35:26 -0600 (Sat, 22 Jan 2011) | 3 lines Fixing problem where reversed colormaps of LinearSegmentedColormaps were not initialized properly. Thanks to LittleBigBrain for reporting and Friedrich Romstedt for making the original patch. ........ svn path=/trunk/matplotlib/; revision=8934
1 parent 5c6f81d commit d26f36f

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

lib/matplotlib/cm.py

+38-18
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def freversed(x):
2525
return freversed
2626

2727
def revcmap(data):
28+
"""Can only handle specification *data* in dictionary format."""
2829
data_r = {}
2930
for key, val in data.iteritems():
3031
if callable(val):
@@ -39,32 +40,51 @@ def revcmap(data):
3940
data_r[key] = valnew
4041
return data_r
4142

43+
def _reverse_cmap_spec(spec):
44+
"""Reverses cmap specification *spec*, can handle both dict and tuple
45+
type specs."""
46+
47+
if 'red' in spec:
48+
return revcmap(spec)
49+
else:
50+
revspec = list(reversed(spec))
51+
if len(revspec[0]) == 2: # e.g., (1, (1.0, 0.0, 1.0))
52+
revspec = [(1.0 - a, b) for a, b in revspec]
53+
return revspec
54+
55+
def _generate_cmap(name, lutsize):
56+
"""Generates the requested cmap from it's name *name*. The lut size is
57+
*lutsize*."""
58+
59+
spec = datad[name]
60+
61+
# Generate the colormap object.
62+
if 'red' in spec:
63+
return colors.LinearSegmentedColormap(name, spec, lutsize)
64+
else:
65+
return colors.LinearSegmentedColormap.from_list(spec, spec, lutsize)
66+
4267
LUTSIZE = mpl.rcParams['image.lut']
4368

4469
_cmapnames = datad.keys() # need this list because datad is changed in loop
4570

71+
# Generate the reversed specifications ...
72+
4673
for cmapname in _cmapnames:
47-
cmapname_r = cmapname+'_r'
48-
cmapspec = datad[cmapname]
49-
if 'red' in cmapspec:
50-
datad[cmapname_r] = revcmap(cmapspec)
51-
cmap_d[cmapname] = colors.LinearSegmentedColormap(
52-
cmapname, cmapspec, LUTSIZE)
53-
cmap_d[cmapname_r] = colors.LinearSegmentedColormap(
54-
cmapname_r, datad[cmapname_r], LUTSIZE)
55-
else:
56-
revspec = list(reversed(cmapspec))
57-
if len(revspec[0]) == 2: # e.g., (1, (1.0, 0.0, 1.0))
58-
revspec = [(1.0 - a, b) for a, b in revspec]
59-
datad[cmapname_r] = revspec
74+
spec = datad[cmapname]
75+
spec_reversed = _reverse_cmap_spec(spec)
76+
datad[cmapname + '_r'] = spec_reversed
77+
78+
# Precache the cmaps with ``lutsize = LUTSIZE`` ...
6079

61-
cmap_d[cmapname] = colors.LinearSegmentedColormap.from_list(
62-
cmapname, cmapspec, LUTSIZE)
63-
cmap_d[cmapname_r] = colors.LinearSegmentedColormap.from_list(
64-
cmapname_r, revspec, LUTSIZE)
80+
# Use datad.keys() to also add the reversed ones added in the section above:
81+
for cmapname in datad.keys():
82+
cmap_d[cmapname] = _generate_cmap(cmapname, LUTSIZE)
6583

6684
locals().update(cmap_d)
6785

86+
# Continue with definitions ...
87+
6888
def register_cmap(name=None, cmap=None, data=None, lut=None):
6989
"""
7090
Add a colormap to the set recognized by :func:`get_cmap`.
@@ -128,7 +148,7 @@ def get_cmap(name=None, lut=None):
128148
if lut is None:
129149
return cmap_d[name]
130150
elif name in datad:
131-
return colors.LinearSegmentedColormap(name, datad[name], lut)
151+
return _generate_cmap(name, lut)
132152
else:
133153
raise ValueError("Colormap %s is not recognized" % name)
134154

0 commit comments

Comments
 (0)