@@ -25,6 +25,7 @@ def freversed(x):
25
25
return freversed
26
26
27
27
def revcmap (data ):
28
+ """Can only handle specification *data* in dictionary format."""
28
29
data_r = {}
29
30
for key , val in data .iteritems ():
30
31
if callable (val ):
@@ -39,32 +40,51 @@ def revcmap(data):
39
40
data_r [key ] = valnew
40
41
return data_r
41
42
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
+
42
67
LUTSIZE = mpl .rcParams ['image.lut' ]
43
68
44
69
_cmapnames = datad .keys () # need this list because datad is changed in loop
45
70
71
+ # Generate the reversed specifications ...
72
+
46
73
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`` ...
60
79
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 )
65
83
66
84
locals ().update (cmap_d )
67
85
86
+ # Continue with definitions ...
87
+
68
88
def register_cmap (name = None , cmap = None , data = None , lut = None ):
69
89
"""
70
90
Add a colormap to the set recognized by :func:`get_cmap`.
@@ -128,7 +148,7 @@ def get_cmap(name=None, lut=None):
128
148
if lut is None :
129
149
return cmap_d [name ]
130
150
elif name in datad :
131
- return colors . LinearSegmentedColormap (name , datad [ name ] , lut )
151
+ return _generate_cmap (name , lut )
132
152
else :
133
153
raise ValueError ("Colormap %s is not recognized" % name )
134
154
0 commit comments