@@ -61,12 +61,6 @@ class ColormapRegistry(Mapping):
6161 r"""
6262 Container for colormaps that are known to Matplotlib by name.
6363
64- .. admonition:: Experimental
65-
66- While we expect the API to be final, we formally mark it as
67- experimental for 3.5 because we want to keep the option to still adapt
68- the API for 3.6 should the need arise.
69-
7064 The universal registry instance is `matplotlib.colormaps`. There should be
7165 no need for users to instantiate `.ColormapRegistry` themselves.
7266
@@ -193,6 +187,38 @@ def unregister(self, name):
193187 "colormap." )
194188 self ._cmaps .pop (name , None )
195189
190+ def get_cmap (self , cmap ):
191+ """
192+ Return a color map specified through *cmap*.
193+
194+ Parameters
195+ ----------
196+ cmap : str or `~matplotlib.colors.Colormap` or None
197+
198+ - if a `.Colormap`, return it
199+ - if a string, look it up in ``mpl.colormaps``
200+ - if None, return the Colormap defined in :rc:`image.cmap`
201+
202+ Returns
203+ -------
204+ Colormap
205+ """
206+ # get the default color map
207+ if cmap is None :
208+ return self [mpl .rcParams ["image.cmap" ]]
209+
210+ # if the user passed in a Colormap, simply return it
211+ if isinstance (cmap , colors .Colormap ):
212+ return cmap
213+ if isinstance (cmap , str ):
214+ _api .check_in_list (sorted (_colormaps ), cmap = cmap )
215+ # otherwise, it must be a string so look it up
216+ return self [cmap ]
217+ raise TypeError (
218+ 'get_cmap expects None or an instance of a str or Colormap . ' +
219+ f'you passed { cmap !r} of type { type (cmap )} '
220+ )
221+
196222
197223# public access to the colormaps should be via `matplotlib.colormaps`. For now,
198224# we still create the registry here, but that should stay an implementation
@@ -281,7 +307,12 @@ def _get_cmap(name=None, lut=None):
281307# pyplot.
282308get_cmap = _api .deprecated (
283309 '3.6' ,
284- name = 'get_cmap' , pending = True , alternative = "``matplotlib.colormaps[name]``"
310+ name = 'get_cmap' ,
311+ pending = True ,
312+ alternative = (
313+ "``matplotlib.colormaps[name]`` " +
314+ "or ``matplotlib.colormaps.get_cmap(obj)``"
315+ )
285316)(_get_cmap )
286317
287318
@@ -687,6 +718,8 @@ def _ensure_cmap(cmap):
687718 """
688719 Ensure that we have a `.Colormap` object.
689720
721+ For internal use to preserve type stability of errors.
722+
690723 Parameters
691724 ----------
692725 cmap : None, str, Colormap
@@ -698,6 +731,7 @@ def _ensure_cmap(cmap):
698731 Returns
699732 -------
700733 Colormap
734+
701735 """
702736 if isinstance (cmap , colors .Colormap ):
703737 return cmap
0 commit comments