15
15
normalization.
16
16
"""
17
17
18
+ from collections .abc import MutableMapping
18
19
import functools
19
20
20
21
import numpy as np
@@ -49,7 +50,7 @@ def revcmap(data):
49
50
LUTSIZE = mpl .rcParams ['image.lut' ]
50
51
51
52
52
- def _gen_cmap_d ():
53
+ def _gen_cmap_registry ():
53
54
"""
54
55
Generate a dict mapping standard colormap names to standard colormaps, as
55
56
well as the reversed colormaps.
@@ -71,10 +72,50 @@ def _gen_cmap_d():
71
72
return cmap_d
72
73
73
74
74
- _cmap_d = _gen_cmap_d ()
75
- locals ().update (_cmap_d )
75
+ class _DeprecatedCmapDictWrapper (MutableMapping ):
76
+ """Dictionary mapping for deprecated _cmap_d access."""
77
+
78
+ def __init__ (self , cmap_registry ):
79
+ self ._cmap_registry = cmap_registry
80
+
81
+ def __delitem__ (self , key ):
82
+ self ._warn_deprecated ()
83
+ self ._cmap_registry .__delitem__ (key )
84
+
85
+ def __getitem__ (self , key ):
86
+ self ._warn_deprecated ()
87
+ return self ._cmap_registry .__getitem__ (key )
88
+
89
+ def __iter__ (self ):
90
+ self ._warn_deprecated ()
91
+ return self ._cmap_registry .__iter__ ()
92
+
93
+ def __len__ (self ):
94
+ self ._warn_deprecated ()
95
+ return self ._cmap_registry .__len__ ()
96
+
97
+ def __setitem__ (self , key , val ):
98
+ self ._warn_deprecated ()
99
+ self ._cmap_registry .__setitem__ (key , val )
100
+
101
+ def get (self , key , default = None ):
102
+ self ._warn_deprecated ()
103
+ return self ._cmap_registry .get (key , default )
104
+
105
+ def _warn_deprecated (self ):
106
+ cbook .warn_deprecated (
107
+ "3.3" ,
108
+ message = "The global colormaps dictionary is no longer "
109
+ "considered public API." ,
110
+ alternative = "Please use register_cmap() and get_cmap() to "
111
+ "access the contents of the dictionary."
112
+ )
113
+
114
+
115
+ _cmap_registry = _gen_cmap_registry ()
116
+ locals ().update (_cmap_registry )
76
117
# This is no longer considered public API
77
- cmap_d = _cmap_d
118
+ cmap_d = _DeprecatedCmapDictWrapper ( _cmap_registry )
78
119
79
120
80
121
# Continue with definitions ...
@@ -99,6 +140,13 @@ def register_cmap(name=None, cmap=None, data=None, lut=None):
99
140
and the resulting colormap is registered. Instead of this implicit
100
141
colormap creation, create a `.LinearSegmentedColormap` and use the first
101
142
case: ``register_cmap(cmap=LinearSegmentedColormap(name, data, lut))``.
143
+
144
+ Notes
145
+ -----
146
+ Registering a colormap stores a reference to the colormap object
147
+ which can currently be modified and inadvertantly change the global
148
+ colormap state. This behavior is deprecated and in Matplotlib 3.5
149
+ the registered colormap will be immutable.
102
150
"""
103
151
cbook ._check_isinstance ((str , None ), name = name )
104
152
if name is None :
@@ -109,7 +157,7 @@ def register_cmap(name=None, cmap=None, data=None, lut=None):
109
157
"Colormap" ) from err
110
158
if isinstance (cmap , colors .Colormap ):
111
159
cmap ._global = True
112
- _cmap_d [name ] = cmap
160
+ _cmap_registry [name ] = cmap
113
161
return
114
162
if lut is not None or data is not None :
115
163
cbook .warn_deprecated (
@@ -123,7 +171,7 @@ def register_cmap(name=None, cmap=None, data=None, lut=None):
123
171
lut = mpl .rcParams ['image.lut' ]
124
172
cmap = colors .LinearSegmentedColormap (name , data , lut )
125
173
cmap ._global = True
126
- _cmap_d [name ] = cmap
174
+ _cmap_registry [name ] = cmap
127
175
128
176
129
177
def get_cmap (name = None , lut = None ):
@@ -133,15 +181,18 @@ def get_cmap(name=None, lut=None):
133
181
Colormaps added with :func:`register_cmap` take precedence over
134
182
built-in colormaps.
135
183
184
+ Notes
185
+ -----
186
+ Currently, this returns the global colormap object, which is deprecated.
187
+ In Matplotlib 3.5, you will no longer be able to modify the global
188
+ colormaps in-place.
189
+
136
190
Parameters
137
191
----------
138
192
name : `matplotlib.colors.Colormap` or str or None, default: None
139
- If a `.Colormap` instance, it will be returned.
140
- Otherwise, the name of a colormap known to Matplotlib, which will
141
- be resampled by *lut*. Currently, this returns the global colormap
142
- object, which is deprecated. In Matplotlib 3.5, you will no longer be
143
- able to modify the global colormaps in-place.
144
- The default, None, means :rc:`image.cmap`.
193
+ If a `.Colormap` instance, it will be returned. Otherwise, the name of
194
+ a colormap known to Matplotlib, which will be resampled by *lut*. The
195
+ default, None, means :rc:`image.cmap`.
145
196
lut : int or None, default: None
146
197
If *name* is not already a Colormap instance and *lut* is not None, the
147
198
colormap will be resampled to have *lut* entries in the lookup table.
@@ -150,11 +201,11 @@ def get_cmap(name=None, lut=None):
150
201
name = mpl .rcParams ['image.cmap' ]
151
202
if isinstance (name , colors .Colormap ):
152
203
return name
153
- cbook ._check_in_list (sorted (_cmap_d ), name = name )
204
+ cbook ._check_in_list (sorted (_cmap_registry ), name = name )
154
205
if lut is None :
155
- return _cmap_d [name ]
206
+ return _cmap_registry [name ]
156
207
else :
157
- return _cmap_d [name ]._resample (lut )
208
+ return _cmap_registry [name ]._resample (lut )
158
209
159
210
160
211
class ScalarMappable :
0 commit comments