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

Backport PR #28355 on branch v3.9.x (MNT: Re-add matplotlib.cm.get_cmap) #28469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions 38 lib/matplotlib/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,44 @@
globals().update(_colormaps)


# This is an exact copy of pyplot.get_cmap(). It 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.
@_api.deprecated(
'3.7',
removal='3.11',
alternative="``matplotlib.colormaps[name]`` or ``matplotlib.colormaps.get_cmap()``"
" or ``pyplot.get_cmap()``"
)
def get_cmap(name=None, lut=None):
"""
Get a colormap instance, defaulting to rc values if *name* is None.

Parameters
----------
name : `~matplotlib.colors.Colormap` or str or None, default: None
If a `.Colormap` instance, it will be returned. Otherwise, the name of
a colormap known to Matplotlib, which will be resampled by *lut*. The
default, None, means :rc:`image.cmap`.
lut : int or None, default: None
If *name* is not already a Colormap instance and *lut* is not None, the
colormap will be resampled to have *lut* entries in the lookup table.

Returns
-------
Colormap
"""
if name is None:
name = mpl.rcParams['image.cmap']

Check warning on line 263 in lib/matplotlib/cm.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/cm.py#L263

Added line #L263 was not covered by tests
if isinstance(name, colors.Colormap):
return name
_api.check_in_list(sorted(_colormaps), name=name)

Check warning on line 266 in lib/matplotlib/cm.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/cm.py#L265-L266

Added lines #L265 - L266 were not covered by tests
if lut is None:
return _colormaps[name]

Check warning on line 268 in lib/matplotlib/cm.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/cm.py#L268

Added line #L268 was not covered by tests
else:
return _colormaps[name].resampled(lut)

Check warning on line 270 in lib/matplotlib/cm.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/cm.py#L270

Added line #L270 was not covered by tests


def _auto_norm_from_scale(scale_cls):
"""
Automatically generate a norm class from *scale_cls*.
Expand Down
2 changes: 2 additions & 0 deletions 2 lib/matplotlib/cm.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class ColormapRegistry(Mapping[str, colors.Colormap]):

_colormaps: ColormapRegistry = ...

def get_cmap(name: str | colors.Colormap | None = ..., lut: int | None = ...) -> colors.Colormap: ...

class ScalarMappable:
cmap: colors.Colormap | None
colorbar: Colorbar | None
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.