@@ -201,6 +201,11 @@ def unregister(self, name):
201201globals ().update (_colormaps )
202202
203203
204+ @_api .deprecated (
205+ '3.6' ,
206+ pending = True ,
207+ alternative = "``matplotlib.colormaps.register_cmap(name)``"
208+ )
204209def register_cmap (name = None , cmap = None , * , override_builtin = False ):
205210 """
206211 Add a colormap to the set recognized by :func:`get_cmap`.
@@ -244,13 +249,10 @@ def register_cmap(name=None, cmap=None, *, override_builtin=False):
244249 _colormaps ._allow_override_builtin = False
245250
246251
247- def get_cmap (name = None , lut = None ):
252+ def _get_cmap (name = None , lut = None ):
248253 """
249254 Get a colormap instance, defaulting to rc values if *name* is None.
250255
251- Colormaps added with :func:`register_cmap` take precedence over
252- built-in colormaps.
253-
254256 Parameters
255257 ----------
256258 name : `matplotlib.colors.Colormap` or str or None, default: None
@@ -260,6 +262,10 @@ def get_cmap(name=None, lut=None):
260262 lut : int or None, default: None
261263 If *name* is not already a Colormap instance and *lut* is not None, the
262264 colormap will be resampled to have *lut* entries in the lookup table.
265+
266+ Returns
267+ -------
268+ Colormap
263269 """
264270 if name is None :
265271 name = mpl .rcParams ['image.cmap' ]
@@ -269,9 +275,20 @@ def get_cmap(name=None, lut=None):
269275 if lut is None :
270276 return _colormaps [name ]
271277 else :
272- return _colormaps [name ]._resample (lut )
278+ return _colormaps [name ].resampled (lut )
273279
280+ # do it in two steps like this so we can have an un-deprecated version in
281+ # pyplot.
282+ get_cmap = _api .deprecated (
283+ '3.6' , pending = True , alternative = "``matplotlib.colormaps[name]``"
284+ )(_get_cmap )
274285
286+
287+ @_api .deprecated (
288+ '3.6' ,
289+ pending = True ,
290+ alternative = "``matplotlib.colormaps.unregister_cmap(name)``"
291+ )
275292def unregister_cmap (name ):
276293 """
277294 Remove a colormap recognized by :func:`get_cmap`.
@@ -550,8 +567,8 @@ def set_cmap(self, cmap):
550567 cmap : `.Colormap` or str or None
551568 """
552569 in_init = self .cmap is None
553- cmap = get_cmap ( cmap )
554- self .cmap = cmap
570+
571+ self .cmap = _ensure_cmap ( cmap )
555572 if not in_init :
556573 self .changed () # Things are not set up properly yet.
557574
@@ -663,3 +680,26 @@ def changed(self):
663680 *vmin*/*vmax* when a *norm* instance is given (but using a `str` *norm*
664681 name together with *vmin*/*vmax* is acceptable).""" ,
665682)
683+
684+
685+ def _ensure_cmap (cmap ):
686+ """
687+ Ensure that we have a `.Colormap` object.
688+
689+ Parameters
690+ ----------
691+ cmap : None, str, Colormap
692+
693+ - if a `Colormap`, return it
694+ - if a string, look it up in mpl.colormaps
695+ - if None, look up the default color map in mpl.colormaps
696+
697+ Returns
698+ -------
699+ Colormap
700+ """
701+ if isinstance (cmap , colors .Colormap ):
702+ return cmap
703+ return mpl .colormaps [
704+ cmap if cmap is not None else mpl .rcParams ['image.cmap' ]
705+ ]
0 commit comments