1515 normalization.
1616"""
1717
18+ from collections .abc import MutableMapping
1819import functools
1920
2021import numpy as np
@@ -49,7 +50,7 @@ def revcmap(data):
4950LUTSIZE = mpl .rcParams ['image.lut' ]
5051
5152
52- def _gen_cmap_d ():
53+ def _gen_cmap_registry ():
5354 """
5455 Generate a dict mapping standard colormap names to standard colormaps, as
5556 well as the reversed colormaps.
@@ -71,10 +72,50 @@ def _gen_cmap_d():
7172 return cmap_d
7273
7374
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 )
76117# This is no longer considered public API
77- cmap_d = _cmap_d
118+ cmap_d = _DeprecatedCmapDictWrapper ( _cmap_registry )
78119
79120
80121# Continue with definitions ...
@@ -99,6 +140,13 @@ def register_cmap(name=None, cmap=None, data=None, lut=None):
99140 and the resulting colormap is registered. Instead of this implicit
100141 colormap creation, create a `.LinearSegmentedColormap` and use the first
101142 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.
102150 """
103151 cbook ._check_isinstance ((str , None ), name = name )
104152 if name is None :
@@ -109,7 +157,7 @@ def register_cmap(name=None, cmap=None, data=None, lut=None):
109157 "Colormap" ) from err
110158 if isinstance (cmap , colors .Colormap ):
111159 cmap ._global = True
112- _cmap_d [name ] = cmap
160+ _cmap_registry [name ] = cmap
113161 return
114162 if lut is not None or data is not None :
115163 cbook .warn_deprecated (
@@ -123,7 +171,7 @@ def register_cmap(name=None, cmap=None, data=None, lut=None):
123171 lut = mpl .rcParams ['image.lut' ]
124172 cmap = colors .LinearSegmentedColormap (name , data , lut )
125173 cmap ._global = True
126- _cmap_d [name ] = cmap
174+ _cmap_registry [name ] = cmap
127175
128176
129177def get_cmap (name = None , lut = None ):
@@ -133,15 +181,18 @@ def get_cmap(name=None, lut=None):
133181 Colormaps added with :func:`register_cmap` take precedence over
134182 built-in colormaps.
135183
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+
136190 Parameters
137191 ----------
138192 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`.
145196 lut : int or None, default: None
146197 If *name* is not already a Colormap instance and *lut* is not None, the
147198 colormap will be resampled to have *lut* entries in the lookup table.
@@ -150,11 +201,11 @@ def get_cmap(name=None, lut=None):
150201 name = mpl .rcParams ['image.cmap' ]
151202 if isinstance (name , colors .Colormap ):
152203 return name
153- cbook ._check_in_list (sorted (_cmap_d ), name = name )
204+ cbook ._check_in_list (sorted (_cmap_registry ), name = name )
154205 if lut is None :
155- return _cmap_d [name ]
206+ return _cmap_registry [name ]
156207 else :
157- return _cmap_d [name ]._resample (lut )
208+ return _cmap_registry [name ]._resample (lut )
158209
159210
160211class ScalarMappable :
0 commit comments