Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aaeb01e

Browse filesBrowse files
authored
Merge pull request #28469 from meeseeksmachine/auto-backport-of-pr-28355-on-v3.9.x
Backport PR #28355 on branch v3.9.x (MNT: Re-add matplotlib.cm.get_cmap)
2 parents e392b59 + 5ad7028 commit aaeb01e
Copy full SHA for aaeb01e

File tree

2 files changed

+40
-0
lines changed
Filter options

2 files changed

+40
-0
lines changed

‎lib/matplotlib/cm.py

Copy file name to clipboardExpand all lines: lib/matplotlib/cm.py
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,44 @@ def get_cmap(self, cmap):
232232
globals().update(_colormaps)
233233

234234

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

‎lib/matplotlib/cm.pyi

Copy file name to clipboardExpand all lines: lib/matplotlib/cm.pyi
+2Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.