diff --git a/doc/api/next_api_changes/removals/30005-DS.rst b/doc/api/next_api_changes/removals/30005-DS.rst new file mode 100644 index 000000000000..a5ba482c848f --- /dev/null +++ b/doc/api/next_api_changes/removals/30005-DS.rst @@ -0,0 +1,11 @@ +``matplotlib.cm.get_cmap`` +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Colormaps are now available through the `.ColormapRegistry` accessible via +`matplotlib.colormaps` or `matplotlib.pyplot.colormaps`. + +If you have the name of a colormap as a string, you can use a direct lookup, +``matplotlib.colormaps[name]`` or ``matplotlib.pyplot.colormaps[name]`` . Alternatively, ``matplotlib.colormaps.get_cmap`` will +maintain the existing behavior of additionally passing through `.Colormap` instances +and converting ``None`` to the default colormap. `matplotlib.pyplot.get_cmap` will stay as a +shortcut to ``matplotlib.colormaps.get_cmap``. diff --git a/lib/matplotlib/cm.py b/lib/matplotlib/cm.py index 2697666b9573..ef5bf0719d3b 100644 --- a/lib/matplotlib/cm.py +++ b/lib/matplotlib/cm.py @@ -243,43 +243,6 @@ def get_cmap(self, cmap): _bivar_colormaps = ColormapRegistry(bivar_cmaps) -# 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 - """ - name = mpl._val_or_rc(name, '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 _ensure_cmap(cmap): """ Ensure that we have a `.Colormap` object. diff --git a/lib/matplotlib/cm.pyi b/lib/matplotlib/cm.pyi index c3c62095684a..366b336fe04d 100644 --- a/lib/matplotlib/cm.pyi +++ b/lib/matplotlib/cm.pyi @@ -19,6 +19,4 @@ _colormaps: ColormapRegistry = ... _multivar_colormaps: ColormapRegistry = ... _bivar_colormaps: ColormapRegistry = ... -def get_cmap(name: str | colors.Colormap | None = ..., lut: int | None = ...) -> colors.Colormap: ... - ScalarMappable = _ScalarMappable