Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4627a5e

Browse files
authored
Merge pull request #24124 from meeseeksmachine/auto-backport-of-pr-24111-on-v3.6.x
Backport PR #24111 on branch v3.6.x (FIX: add missing method to ColormapRegistry)
2 parents 78bcd91 + 3863297 commit 4627a5e

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

doc/api/prev_api_changes/api_changes_3.6.0/deprecations.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ In Matplotlib 3.6 we have marked those top level functions as pending
5252
deprecation with the intention of deprecation in Matplotlib 3.7. The following
5353
functions have been marked for pending deprecation:
5454

55-
- ``matplotlib.cm.get_cmap``; use ``matplotlib.colormaps[name]`` instead
55+
- ``matplotlib.cm.get_cmap``; use ``matplotlib.colormaps[name]`` instead if you
56+
have a `str`.
57+
58+
**Added 3.6.1** Use `matplotlib.cm.ColormapRegistry.get_cmap` if you
59+
have a string, `None` or a `matplotlib.colors.Colormap` object that you want
60+
to convert to a `matplotlib.colors.Colormap` instance.
5661
- ``matplotlib.cm.register_cmap``; use `matplotlib.colormaps.register
5762
<.ColormapRegistry.register>` instead
5863
- ``matplotlib.cm.unregister_cmap``; use `matplotlib.colormaps.unregister
@@ -305,7 +310,7 @@ Backend-specific deprecations
305310
private functions if you rely on it.
306311
- ``backend_svg.generate_transform`` and ``backend_svg.generate_css``
307312
- ``backend_tk.NavigationToolbar2Tk.lastrect`` and
308-
``backend_tk.RubberbandTk.lastrect``
313+
``backend_tk.RubberbandTk.lastrect``
309314
- ``backend_tk.NavigationToolbar2Tk.window``; use ``toolbar.master`` instead.
310315
- ``backend_tools.ToolBase.destroy``; To run code upon tool removal, connect to
311316
the ``tool_removed_event`` event.

lib/matplotlib/cm.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
282308
get_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

lib/matplotlib/tests/test_colors.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ def test_register_cmap():
109109
cm.register_cmap('nome', cmap='not a cmap')
110110

111111

112+
def test_colormaps_get_cmap():
113+
cr = mpl.colormaps
114+
115+
# check str, and Colormap pass
116+
assert cr.get_cmap('plasma') == cr["plasma"]
117+
assert cr.get_cmap(cr["magma"]) == cr["magma"]
118+
119+
# check default
120+
assert cr.get_cmap(None) == cr[mpl.rcParams['image.cmap']]
121+
122+
# check ValueError on bad name
123+
bad_cmap = 'AardvarksAreAwkward'
124+
with pytest.raises(ValueError, match=bad_cmap):
125+
cr.get_cmap(bad_cmap)
126+
127+
# check TypeError on bad type
128+
with pytest.raises(TypeError, match='object'):
129+
cr.get_cmap(object())
130+
131+
112132
def test_double_register_builtin_cmap():
113133
name = "viridis"
114134
match = f"Re-registering the builtin cmap {name!r}."

0 commit comments

Comments
 (0)