13
13
14
14
:doc:`/tutorials/colors/colormapnorms` for more details about data
15
15
normalization.
16
-
17
-
18
16
"""
19
17
20
18
import functools
29
27
from matplotlib ._cm_listed import cmaps as cmaps_listed
30
28
31
29
32
- cmap_d = {}
33
-
34
-
35
- # reverse all the colormaps.
36
- # reversed colormaps have '_r' appended to the name.
37
-
38
-
39
- def _reverser (f , x ): # Toplevel helper for revcmap ensuring cmap picklability.
40
- return f (1 - x )
30
+ def _reverser (f , x ): # Deprecated, remove this at the same time as revcmap.
31
+ return f (1 - x ) # Toplevel helper for revcmap ensuring cmap picklability.
41
32
42
33
34
+ @cbook .deprecated ("3.2" , alternative = "Colormap.reversed()" )
43
35
def revcmap (data ):
44
36
"""Can only handle specification *data* in dictionary format."""
45
37
data_r = {}
@@ -54,51 +46,30 @@ def revcmap(data):
54
46
return data_r
55
47
56
48
57
- def _reverse_cmap_spec (spec ):
58
- """Reverses cmap specification *spec*, can handle both dict and tuple
59
- type specs."""
60
-
61
- if 'listed' in spec :
62
- return {'listed' : spec ['listed' ][::- 1 ]}
63
-
64
- if 'red' in spec :
65
- return revcmap (spec )
66
- else :
67
- revspec = list (reversed (spec ))
68
- if len (revspec [0 ]) == 2 : # e.g., (1, (1.0, 0.0, 1.0))
69
- revspec = [(1.0 - a , b ) for a , b in revspec ]
70
- return revspec
71
-
72
-
73
- def _generate_cmap (name , lutsize ):
74
- """Generates the requested cmap from its *name*. The lut size is
75
- *lutsize*."""
76
-
77
- spec = datad [name ]
78
-
79
- # Generate the colormap object.
80
- if 'red' in spec :
81
- return colors .LinearSegmentedColormap (name , spec , lutsize )
82
- elif 'listed' in spec :
83
- return colors .ListedColormap (spec ['listed' ], name )
84
- else :
85
- return colors .LinearSegmentedColormap .from_list (name , spec , lutsize )
86
-
87
-
88
49
LUTSIZE = mpl .rcParams ['image.lut' ]
89
50
90
- # Generate the reversed specifications (all at once, to avoid
91
- # modify-when-iterating).
92
- datad .update ({cmapname + '_r' : _reverse_cmap_spec (spec )
93
- for cmapname , spec in datad .items ()})
94
-
95
- # Precache the cmaps with ``lutsize = LUTSIZE``.
96
- # Also add the reversed ones added in the section above:
97
- for cmapname in datad :
98
- cmap_d [cmapname ] = _generate_cmap (cmapname , LUTSIZE )
99
-
100
- cmap_d .update (cmaps_listed )
101
51
52
+ def _gen_cmap_d ():
53
+ """
54
+ Generate a dict mapping standard colormap names to standard colormaps, as
55
+ well as the reversed colormaps.
56
+ """
57
+ cmap_d = {** cmaps_listed }
58
+ for name , spec in datad .items ():
59
+ cmap_d [name ] = ( # Precache the cmaps at a fixed lutsize..
60
+ colors .LinearSegmentedColormap (name , spec , LUTSIZE )
61
+ if 'red' in spec else
62
+ colors .ListedColormap (spec ['listed' ], name )
63
+ if 'listed' in spec else
64
+ colors .LinearSegmentedColormap .from_list (name , spec , LUTSIZE ))
65
+ # Generate reversed cmaps.
66
+ for cmap in list (cmap_d .values ()):
67
+ rmap = cmap .reversed ()
68
+ cmap_d [rmap .name ] = rmap
69
+ return cmap_d
70
+
71
+
72
+ cmap_d = _gen_cmap_d ()
102
73
locals ().update (cmap_d )
103
74
104
75
0 commit comments