Skip to content

Fix cmap deprecations #23710

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 3 commits into from
Aug 23, 2022
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
7 changes: 7 additions & 0 deletions doc/api/next_api_changes/behavior/23710-ES.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
``plt.get_cmap`` and ``matplotlib.cm.get_cmap`` return a copy
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Formerly, `~.pyplot.get_cmap` and `.cm.get_cmap` returned a global version of a
`.Colormap`. This was prone to errors as modification of the colormap would
propagate from one location to another without warning. Now, a new copy of the
colormap is returned.
7 changes: 4 additions & 3 deletions lib/matplotlib/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def unregister(self, name):
@_api.deprecated(
'3.6',
pending=True,
alternative="``matplotlib.colormaps.register_cmap(name)``"
alternative="``matplotlib.colormaps.register(name)``"
)
def register_cmap(name=None, cmap=None, *, override_builtin=False):
"""
Expand Down Expand Up @@ -280,14 +280,15 @@ def _get_cmap(name=None, lut=None):
# do it in two steps like this so we can have an un-deprecated version in
# pyplot.
get_cmap = _api.deprecated(
'3.6', pending=True, alternative="``matplotlib.colormaps[name]``"
'3.6',
name='get_cmap', pending=True, alternative="``matplotlib.colormaps[name]``"
)(_get_cmap)


@_api.deprecated(
'3.6',
pending=True,
alternative="``matplotlib.colormaps.unregister_cmap(name)``"
alternative="``matplotlib.colormaps.unregister(name)``"
)
def unregister_cmap(name):
"""
Expand Down
10 changes: 5 additions & 5 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_register_cmap():
target = "viridis2"
with pytest.warns(
PendingDeprecationWarning,
match=r"matplotlib\.colormaps\.register_cmap\(name\)"
match=r"matplotlib\.colormaps\.register\(name\)"
):
cm.register_cmap(target, new_cm)
assert mpl.colormaps[target] == new_cm
Expand All @@ -78,13 +78,13 @@ def test_register_cmap():
match="Arguments must include a name or a Colormap"):
with pytest.warns(
PendingDeprecationWarning,
match=r"matplotlib\.colormaps\.register_cmap\(name\)"
match=r"matplotlib\.colormaps\.register\(name\)"
):
cm.register_cmap()

with pytest.warns(
PendingDeprecationWarning,
match=r"matplotlib\.colormaps\.unregister_cmap\(name\)"
match=r"matplotlib\.colormaps\.unregister\(name\)"
):
cm.unregister_cmap(target)
with pytest.raises(ValueError,
Expand All @@ -96,15 +96,15 @@ def test_register_cmap():
cm.get_cmap(target)
with pytest.warns(
PendingDeprecationWarning,
match=r"matplotlib\.colormaps\.unregister_cmap\(name\)"
match=r"matplotlib\.colormaps\.unregister\(name\)"
):
# test that second time is error free
cm.unregister_cmap(target)

with pytest.raises(TypeError, match="'cmap' must be"):
with pytest.warns(
PendingDeprecationWarning,
match=r"matplotlib\.colormaps\.register_cmap\(name\)"
match=r"matplotlib\.colormaps\.register\(name\)"
):
cm.register_cmap('nome', cmap='not a cmap')

Expand Down