From f2663ed0214c7a8a2cc5d520a7e9dae467220907 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Mon, 22 Aug 2022 20:00:39 -0400 Subject: [PATCH 1/3] Fix get_cmap name in deprecation message --- lib/matplotlib/cm.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/cm.py b/lib/matplotlib/cm.py index 6001683358f1..c5e7e52d8c51 100644 --- a/lib/matplotlib/cm.py +++ b/lib/matplotlib/cm.py @@ -280,7 +280,8 @@ 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) From f2ca9f9db9fccc086c3556877a1282fb58086d9a Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Mon, 22 Aug 2022 20:02:05 -0400 Subject: [PATCH 2/3] Fix alternatives for cmap registration deprecations --- lib/matplotlib/cm.py | 4 ++-- lib/matplotlib/tests/test_colors.py | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/cm.py b/lib/matplotlib/cm.py index c5e7e52d8c51..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): """ @@ -288,7 +288,7 @@ def _get_cmap(name=None, lut=None): @_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') From 2a1da87b73fb4b92df36660309088579d07f45d0 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Mon, 22 Aug 2022 20:39:04 -0400 Subject: [PATCH 3/3] Document that get_cmap returns a copy now This was put into effect in #22298, but was not given an API note. --- doc/api/next_api_changes/behavior/23710-ES.rst | 7 +++++++ 1 file changed, 7 insertions(+) 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.