Skip to content
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

FIX: add missing method to ColormapRegistry #24111

Merged
merged 5 commits into from Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions doc/api/prev_api_changes/api_changes_3.6.0/deprecations.rst
Expand Up @@ -52,7 +52,13 @@ In Matplotlib 3.6 we have marked those top level functions as pending
deprecation with the intention of deprecation in Matplotlib 3.7. The following
functions have been marked for pending deprecation:

- ``matplotlib.cm.get_cmap``; use ``matplotlib.colormaps[name]`` instead
- ``matplotlib.cm.get_cmap``; use ``matplotlib.colormaps[name]`` instead if you
have a `str`.

**Added 3.6.1** Use `matplotlib.cm.ColormapRegistry.get_cmap` if you
have a string, `None` or a `matplotlib.colors.Colormap` object that you want
to convert to a `matplotlib.colors.Colormap` instance. Raises `KeyError`
rather than `ValueError` for missing strings.
- ``matplotlib.cm.register_cmap``; use `matplotlib.colormaps.register
<.ColormapRegistry.register>` instead
- ``matplotlib.cm.unregister_cmap``; use `matplotlib.colormaps.unregister
Expand Down Expand Up @@ -305,7 +311,7 @@ Backend-specific deprecations
private functions if you rely on it.
- ``backend_svg.generate_transform`` and ``backend_svg.generate_css``
- ``backend_tk.NavigationToolbar2Tk.lastrect`` and
``backend_tk.RubberbandTk.lastrect``
``backend_tk.RubberbandTk.lastrect``
- ``backend_tk.NavigationToolbar2Tk.window``; use ``toolbar.master`` instead.
- ``backend_tools.ToolBase.destroy``; To run code upon tool removal, connect to
the ``tool_removed_event`` event.
Expand Down
49 changes: 42 additions & 7 deletions lib/matplotlib/cm.py
Expand Up @@ -61,12 +61,6 @@ class ColormapRegistry(Mapping):
r"""
Container for colormaps that are known to Matplotlib by name.

.. admonition:: Experimental

While we expect the API to be final, we formally mark it as
experimental for 3.5 because we want to keep the option to still adapt
the API for 3.6 should the need arise.

The universal registry instance is `matplotlib.colormaps`. There should be
no need for users to instantiate `.ColormapRegistry` themselves.

Expand Down Expand Up @@ -193,6 +187,39 @@ def unregister(self, name):
"colormap.")
self._cmaps.pop(name, None)

def get_cmap(self, cmap):
"""
Ensure that at given object is a converted to a color map.

If *cmap* in `None`, returns the Colormap named by :rc:`image.cmap`.
tacaswell marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
cmap : str, Colormap, None

- if a `~matplotlib.colors.Colormap`, return it
- if a string, look it up in mpl.colormaps
- if None, look up the default color map in mpl.colormaps
tacaswell marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
Colormap

Raises
------
KeyError
QuLogic marked this conversation as resolved.
Show resolved Hide resolved
"""
# get the default color map
if cmap is None:
return self[mpl.rcParams["image.cmap"]]

# if the user passed in a Colormap, simply return it
if isinstance(cmap, colors.Colormap):
return cmap

# otherwise, it must be a string so look it up
return self[cmap]


# public access to the colormaps should be via `matplotlib.colormaps`. For now,
# we still create the registry here, but that should stay an implementation
Expand Down Expand Up @@ -281,7 +308,12 @@ def _get_cmap(name=None, lut=None):
# pyplot.
get_cmap = _api.deprecated(
'3.6',
name='get_cmap', pending=True, alternative="``matplotlib.colormaps[name]``"
name='get_cmap',
pending=True,
alternative=(
"``matplotlib.colormaps[name]`` " +
"or ``matplotlib.colormaps.get_cmap(obj)``"
)
)(_get_cmap)


Expand Down Expand Up @@ -687,6 +719,8 @@ def _ensure_cmap(cmap):
"""
Ensure that we have a `.Colormap` object.

For internal use to preserve type stability of errors.

Parameters
----------
cmap : None, str, Colormap
Expand All @@ -698,6 +732,7 @@ def _ensure_cmap(cmap):
Returns
-------
Colormap

"""
if isinstance(cmap, colors.Colormap):
return cmap
Expand Down
15 changes: 15 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Expand Up @@ -109,6 +109,21 @@ def test_register_cmap():
cm.register_cmap('nome', cmap='not a cmap')


def test_ensure_cmap():
tacaswell marked this conversation as resolved.
Show resolved Hide resolved
cr = mpl.colormaps
new_cm = mcolors.ListedColormap(cr["viridis"].colors, name='v2')
tacaswell marked this conversation as resolved.
Show resolved Hide resolved

# check None, str, and Colormap pass
tacaswell marked this conversation as resolved.
Show resolved Hide resolved
assert cr.get_cmap('plasma') == cr["plasma"]
assert cr.get_cmap(cr["magma"]) == cr["magma"]

# check default default
assert cr.get_cmap(None) == cr[mpl.rcParams['image.cmap']]
bad_cmap = 'AardvarksAreAwkward'
with pytest.raises(KeyError, match=bad_cmap):
cr.get_cmap(bad_cmap)


def test_double_register_builtin_cmap():
name = "viridis"
match = f"Re-registering the builtin cmap {name!r}."
Expand Down