8000 Merge pull request #23710 from QuLogic/fix-cmap-deprecations · matplotlib/matplotlib@1368cfe · GitHub
[go: up one dir, main page]

Skip to content

Commit 1368cfe

Browse files
authored
Merge pull request #23710 from QuLogic/fix-cmap-deprecations
Fix cmap deprecations
2 parents 5773bf9 + 2a1da87 commit 1368cfe

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
``plt.get_cmap`` and ``matplotlib.cm.get_cmap`` return a copy
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Formerly, `~.pyplot.get_cmap` and `.cm.get_cmap` returned a global version of a
5+
`.Colormap`. This was prone to errors as modification of the colormap would
6+
propagate from one location to another without warning. Now, a new copy of the
7+
colormap is returned.

lib/matplotlib/cm.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def unregister(self, name):
204204
@_api.deprecated(
205205
'3.6',
206206
pending=True,
207-
alternative="``matplotlib.colormaps.register_cmap(name)``"
207+
alternative="``matplotlib.colormaps.register(name)``"
208208
)
209209
def register_cmap(name=None, cmap=None, *, override_builtin=False):
210210
"""
@@ -280,14 +280,15 @@ def _get_cmap(name=None, lut=None):
280280
# do it in two steps like this so we can have an un-deprecated version in
281281
# pyplot.
282282
get_cmap = _api.deprecated(
283-
'3.6', pending=True, alternative="``matplotlib.colormaps[name]``"
283+
'3.6',
284+
name='get_cmap', pending=True, alternative="``matplotlib.colormaps[name]``"
284285
)(_get_cmap)
285286

286287

287288
@_api.deprecated(
288289
'3.6',
289290
pending=True,
290-
alternative="``matplotlib.colormaps.unregister_cmap(name)``"
291+
alternative="``matplotlib.colormaps.unregister(name)``"
291292
)
292293
def unregister_cmap(name):
293294
"""

lib/matplotlib/tests/test_colors.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def test_register_cmap():
6969
target = "viridis2"
7070
with pytest.warns(
7171
PendingDeprecationWarning,
72-
match=r"matplotlib\.colormaps\.register_cmap\(name\)"
72+
match=r"matplotlib\.colormaps\.register\(name\)"
7373
):
7474
cm.register_cmap(target, new_cm)
7575
assert mpl.colormaps[target] == new_cm
@@ -78,13 +78,13 @@ def test_register_cmap():
7878
match="Arguments must include a name or a Colormap"):
7979
with pytest.warns(
8080
PendingDeprecationWarning,
81-
match=r"matplotlib\.colormaps\.register_cmap\(name\)"
81+
match=r"matplotlib\.colormaps\.register\(name\)"
8282
):
8383
cm.register_cmap()
8484

8585
with pytest.warns(
8686
PendingDeprecationWarning,
87-
match=r"matplotlib\.colormaps\.unregister_cmap\(name\)"
87+
match=r"matplotlib\.colormaps\.unregister\(name\)"
8888
):
8989
cm.unregister_cmap(target)
9090
with pytest.raises(ValueError,
@@ -96,15 +96,15 @@ def test_register_cmap():
9696
cm.get_cmap(target)
9797
with pytest.warns(
9898
PendingDeprecationWarning,
99-
match=r"matplotlib\.colormaps\.unregister_cmap\(name\)"
99+
match=r"matplotlib\.colormaps\.unregister\(name\)"
100100
):
101101
# test that second time is error free
102102
cm.unregister_cmap(target)
103103

104104
with pytest.raises(TypeError, match="'cmap' must be"):
105105
with pytest.warns(
106106
PendingDeprecationWarning,
107-
match=r"matplotlib\.colormaps\.register_cmap\(name\)"
107+
match=r"matplotlib\.colormaps\.register\(name\)"
108108
):
109109
cm.register_cmap('nome', cmap='not a cmap')
110110

0 commit comments

Comments
 (0)
0