8000 MNT: Re-add matplotlib.cm.get_cmap · matplotlib/matplotlib@ef6fa3d · GitHub
[go: up one dir, main page]

Skip to content

Commit ef6fa3d

Browse files
committed
MNT: Re-add matplotlib.cm.get_cmap
This was removed in 3.9, but apparently caused more user trouble than expected. Re-added for 3.9.1 and extended the deprecation period for two additional minor releases.
1 parent 7ccfd38 commit ef6fa3d

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/matplotlib/cm.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,44 @@ def get_cmap(self, cmap):
239239
globals().update(_colormaps)
240240

241241

242+
# This is an exact copy of pyplot.get_cmap(). It was removed in 3.9, but apparently
243+
# caused more user trouble than expected. Re-added for 3.9.1 and extended the
244+
# deprecation period for two additional minor releases.
245+
@_api.deprecated(
246+
'3.7',
247+
removal='3.11',
248+
alternative="``matplotlib.colormaps[name]`` or ``matplotlib.colormaps.get_cmap()``"
249+
" or ``pyplot.get_cmap()``"
250+
)
251+
def get_cmap(name=None, lut=None):
252+
"""
253+
Get a colormap instance, defaulting to rc values if *name* is None.
254+
255+
Parameters
256+
----------
257+
name : `~matplotlib.colors.Colormap` or str or None, default: None
258+
If a `.Colormap` instance, it will be returned. Otherwise, the name of
259+
a colormap known to Matplotlib, which will be resampled by *lut*. The
260+
default, None, means :rc:`image.cmap`.
261+
lut : int or None, default: None
262+
If *name* is not already a Colormap instance and *lut* is not None, the
263+
colormap will be resampled to have *lut* entries in the lookup table.
264+
265+
Returns
266+
-------
267+
Colormap
268+
"""
269+
if name is None:
270+
name = mpl.rcParams['image.cmap']
271+
if isinstance(name, colors.Colormap):
272+
return name
273+
_api.check_in_list(sorted(_colormaps), name=name)
274+
if lut is None:
275+
return _colormaps[name]
276+
else:
277+
return _colormaps[name].resampled(lut)
278+
279+
242280
def _auto_norm_from_scale(scale_cls):
243281
"""
244282
Automatically generate a norm class from *scale_cls*.

lib/matplotlib/cm.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class ColormapRegistry(Mapping[str, colors.Colormap]):
1919

2020
_colormaps: ColormapRegistry = ...
2121

22+
def get_cmap(name: str | colors.Colormap | None = ..., lut: int | None = ...) -> colors.Colormap: ...
23+
2224
class ScalarMappable:
2325
cmap: colors.Colormap | None
2426
colorbar: Colorbar | None

0 commit comments

Comments
 (0)
0