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,48 @@ 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 _DeprecatedCmapDict (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
+ return self ._cmap_registry .__iter__ ()
91
+
92
+ def __len__ (self ):
93
+ return self ._cmap_registry .__len__ ()
94
+
95
+ def __setitem__ (self , key , val ):
96
+ self ._warn_deprecated ()
97
+ self ._cmap_registry .__setitem__ (key , val )
98
+
99
+ def get (self , key , default = None ):
100
+ self ._warn_deprecated ()
101
+ return self ._cmap_registry .get (key , default )
102
+
103
+ def _warn_deprecated (self ):
104
+ cbook .warn_deprecated (
105
+ "3.3" ,
106
+ message = "The global colormaps dictionary is no longer "
107
+ "considered public API." ,
108
+ alternative = "Please use register_cmap() and get_cmap() to "
109
+ "access the contents of the dictionary."
110
+ )
111
+
112
+
113
+ _cmap_registry = _gen_cmap_registry ()
114
+ locals ().update (_cmap_registry )
76
115
# This is no longer considered public API
77
- cmap_d = _cmap_d
116
+ cmap_d = _DeprecatedCmapDict ( _cmap_registry )
78
117
79
118
80
119
# Continue with definitions ...
@@ -99,6 +138,13 @@ def register_cmap(name=None, cmap=None, data=None, lut=None):
99
138
and the resulting colormap is registered. Instead of this implicit
100
139
colormap creation, create a `.LinearSegmentedColormap` and use the first
101
140
case: ``register_cmap(cmap=LinearSegmentedColormap(name, data, lut))``.
141
+
142
+ Notes
143
+ -----
144
+ Registering a colormap stores a reference to the colormap object
145
+ which can currently be modified and inadvertantly change the global
146
+ colormap state. This behavior is deprecated and in Matplotlib 3.5
147
+ the registered colormap will be immutable.
102
148
"""
103
149
cbook ._check_isinstance ((str , None ), name = name )
104
150
if name is None :
@@ -109,7 +155,7 @@ def register_cmap(name=None, cmap=None, data=None, lut=None):
109
155
"Colormap" ) from err
110
156
if isinstance (cmap , colors .Colormap ):
111
157
cmap ._global = True
112
- _cmap_d [name ] = cmap
158
+ _cmap_registry [name ] = cmap
113
159
return
114
160
if lut is not None or data is not None :
115
161
cbook .warn_deprecated (
@@ -123,7 +169,7 @@ def register_cmap(name=None, cmap=None, data=None, lut=None):
123
169
lut = mpl .rcParams ['image.lut' ]
124
170
cmap = colors .LinearSegmentedColormap (name , data , lut )
125
171
cmap ._global = True
126
- _cmap_d [name ] = cmap
172
+ _cmap_registry [name ] = cmap
127
173
128
174
129
175
def get_cmap (name = None , lut = None ):
@@ -133,15 +179,18 @@ def get_cmap(name=None, lut=None):
133
179
Colormaps added with :func:`register_cmap` take precedence over
134
180
built-in colormaps.
135
181
182
+ Notes
183
+ -----
184
+ Currently, this returns the global colormap object, which is deprecated.
185
+ In Matplotlib 3.5, you will no longer be able to modify the global
186
+ colormaps in-place.
187
+
136
188
Parameters
137
189
----------
138
190
name : `matplotlib.colors.Colormap` or str or None, default: None
139
191
If a `.Colormap` instance, it will be returned.
140
192
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
+ be resampled by *lut*. The default, None, means :rc:`image.cmap`.
145
194
lut : int or None, default: None
146
195
If *name* is not already a Colormap instance and *lut* is not None, the
147
196
colormap will be resampled to have *lut* entries in the lookup table.
@@ -150,11 +199,11 @@ def get_cmap(name=None, lut=None):
150
199
name = mpl .rcParams ['image.cmap' ]
151
200
if isinstance (name , colors .Colormap ):
152
201
return name
153
- cbook ._check_in_list (sorted (_cmap_d ), name = name )
202
+ cbook ._check_in_list (sorted (_cmap_registry ), name = name )
154
203
if lut is None :
155
- return _cmap_d [name ]
204
+ return _cmap_registry [name ]
156
205
else :
157
- return _cmap_d [name ]._resample (lut )
206
+ return _cmap_registry [name ]._resample (lut )
158
207
159
208
160
209
class ScalarMappable :
0 commit comments