From a26402d2cfaf9ed7b02ce55cd0969eca87494321 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Tue, 23 Aug 2022 06:24:59 +0200 Subject: [PATCH] Backport PR #23710: Fix cmap deprecations --- doc/api/next_api_changes/behavior/23710-ES.rst | 7 +++++++ lib/matplotlib/cm.py | 7 ++++--- lib/matplotlib/tests/test_colors.py | 10 +++++----- 3 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 doc/api/next_api_changes/behavior/23710-ES.rst diff --git a/doc/api/next_api_changes/behavior/23710-ES.rst b/doc/api/next_api_changes/behavior/23710-ES.rst new file mode 100644 index 000000000000..6b417167a149 --- /dev/null +++ b/doc/api/next_api_changes/behavior/23710-ES.rst @@ -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. diff --git a/lib/matplotlib/cm.py b/lib/matplotlib/cm.py index 6001683358f1..09a6be9e5ce5 100644 --- a/lib/matplotlib/cm.py +++ b/lib/matplotlib/cm.py @@ -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): """ @@ -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): """ diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 30f28fc67ab7..363ae645bb9e 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -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 @@ -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, @@ -96,7 +96,7 @@ 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) @@ -104,7 +104,7 @@ def test_register_cmap(): 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')
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: