Skip to content

Commit 90dba9f

Browse files
committed
API: Add pending deprecation to mpl.cm top level functions
- .get_cmap - .register_cmap - .unregister_cmap in preference for working with the ColormapRegistry on the top level module.
1 parent fe14b5b commit 90dba9f

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Deprecated top-level cmap registration and access functions in ``mpl.cm``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
As part of `multi-step process
5+
<https://github.com/matplotlib/matplotlib/issues/20853>`_ we are refactoring
6+
global state for managing the registered colormap.
7+
8+
In Matplotlib 3.5 we added a `.ColormapRegistry` class and exposed an
9+
instance at the top level as ``matplotlib.colormaps``. The existing
10+
top level functions in `matplotlib.cm` (``get_cmap``, ``register_cmap``,
11+
``unregister_cmap``) were changed to be aliases around the same instance.
12+
13+
In Matplotlib 3.6 we have marked those top level functions as pending
14+
deprecation with the intention of deprecation in Matplotlib 3.7.

lib/matplotlib/cm.py

+15
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ def unregister(self, name):
201201
globals().update(_colormaps)
202202

203203

204+
@_api.deprecated(
205+
'3.6',
206+
pending=True,
207+
alternative="``matplotilb.colormaps.unregister_cmap(name)``"
208+
)
204209
def register_cmap(name=None, cmap=None, *, override_builtin=False):
205210
"""
206211
Add a colormap to the set recognized by :func:`get_cmap`.
@@ -244,6 +249,11 @@ def register_cmap(name=None, cmap=None, *, override_builtin=False):
244249
_colormaps._allow_override_builtin = False
245250

246251

252+
@_api.deprecated(
253+
'3.6',
254+
pending=True,
255+
alternative="``matplotilb.colormaps[name]``"
256+
)
247257
def get_cmap(name=None, lut=None):
248258
"""
249259
Get a colormap instance, defaulting to rc values if *name* is None.
@@ -272,6 +282,11 @@ def get_cmap(name=None, lut=None):
272282
return _colormaps[name].resample(lut)
273283

274284

285+
@_api.deprecated(
286+
'3.6',
287+
pending=True,
288+
alternative="``matplotilb.colormaps.unregister_cmap(name)``"
289+
)
275290
def unregister_cmap(name):
276291
"""
277292
Remove a colormap recognized by :func:`get_cmap`.

lib/matplotlib/tests/test_colors.py

+22-13
Original file line numberDiff line numberDiff line change
@@ -65,44 +65,53 @@ def test_resample():
6565

6666

6767
def test_register_cmap():
68-
new_cm = cm.get_cmap("viridis")
68+
new_cm = mpl.colormaps["viridis"]
6969
target = "viridis2"
70-
cm.register_cmap(target, new_cm)
71-
assert plt.get_cmap(target) == new_cm
70+
with pytest.warns():
71+
cm.register_cmap(target, new_cm)
72+
assert mpl.colormaps[target] == new_cm
7273

7374
with pytest.raises(ValueError,
7475
match="Arguments must include a name or a Colormap"):
75-
cm.register_cmap()
76+
with pytest.warns():
77+
cm.register_cmap()
7678

77-
cm.unregister_cmap(target)
79+
with pytest.warns():
80+
cm.unregister_cmap(target)
7881
with pytest.raises(ValueError,
7982
match=f'{target!r} is not a valid value for name;'):
80-
cm.get_cmap(target)
81-
# test that second time is error free
82-
cm.unregister_cmap(target)
83+
with pytest.warns():
84+
cm.get_cmap(target)
85+
with pytest.warns():
86+
# test that second time is error free
87+
cm.unregister_cmap(target)
8388

8489
with pytest.raises(TypeError, match="'cmap' must be"):
85-
cm.register_cmap('nome', cmap='not a cmap')
90+
with pytest.warns():
91+
cm.register_cmap('nome', cmap='not a cmap')
8692

8793

8894
def test_double_register_builtin_cmap():
8995
name = "viridis"
9096
match = f"Re-registering the builtin cmap {name!r}."
9197
with pytest.raises(ValueError, match=match):
9298
matplotlib.colormaps.register(
93-
cm.get_cmap(name), name=name, force=True
99+
mpl.colormaps[name], name=name, force=True
94100
)
95101
with pytest.raises(ValueError, match='A colormap named "viridis"'):
96-
cm.register_cmap(name, cm.get_cmap(name))
102+
with pytest.warns():
103+
cm.register_cmap(name, mpl.colormaps[name])
97104
with pytest.warns(UserWarning):
98-
cm.register_cmap(name, cm.get_cmap(name), override_builtin=True)
105+
# TODO is warning more than once!
106+
cm.register_cmap(name, mpl.colormaps[name], override_builtin=True)
99107

100108

101109
def test_unregister_builtin_cmap():
102110
name = "viridis"
103111
match = f'cannot unregister {name!r} which is a builtin colormap.'
104112
with pytest.raises(ValueError, match=match):
105-
cm.unregister_cmap(name)
113+
with pytest.warns():
114+
cm.unregister_cmap(name)
106115

107116

108117
def test_colormap_copy():

0 commit comments

Comments
 (0)