Skip to content

Commit

Permalink
Backport PR matplotlib#28355: MNT: Re-add matplotlib.cm.get_cmap
Browse files Browse the repository at this point in the history
  • Loading branch information
ksunden authored and meeseeksmachine committed Jun 26, 2024
1 parent e392b59 commit 5ad7028
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/matplotlib/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,44 @@ def get_cmap(self, cmap):
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']
if isinstance(name, colors.Colormap):
return name
_api.check_in_list(sorted(_colormaps), name=name)
if lut is None:
return _colormaps[name]
else:
return _colormaps[name].resampled(lut)


def _auto_norm_from_scale(scale_cls):
"""
Automatically generate a norm class from *scale_cls*.
Expand Down
2 changes: 2 additions & 0 deletions 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

0 comments on commit 5ad7028

Please sign in to comment.