Skip to content

Commit f2f5ca2

Browse files
committed
ENH: add method to un-register a color map
1 parent 8167da6 commit f2f5ca2

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
Add ``cm.unregister_cmap`` function
3+
-----------------------------------
4+
5+
`.cm.unregister_cmap` allows users to remove a color map that they
6+
have previously registered.

lib/matplotlib/cm.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,28 @@ def get_cmap(name=None, lut=None):
144144
return cmap_d[name]._resample(lut)
145145

146146

147+
def unregister_cmap(name):
148+
"""
149+
Remove a colormap recognized by :func:`get_cmap`.
150+
151+
You may not remove built in color maps.
152+
153+
If the named color map is not registered, returns with no error, raises
154+
if you try to de-register a default color map.
155+
156+
Raises
157+
------
158+
ValueError
159+
If you
160+
"""
161+
if name not in cmap_d:
162+
return
163+
if name in globals():
164+
raise ValueError(f"Can not unregister {name} which is a builtin "
165+
"color map.")
166+
cmap_d.pop(name)
167+
168+
147169
class ScalarMappable:
148170
"""
149171
This is a mixin class to support scalar data to RGBA mapping.

lib/matplotlib/tests/test_cm.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,15 @@ def test_register_cmap():
1313

1414
with pytest.warns(UserWarning):
1515
mcm.register_cmap('viridis'[::-1], mcm.get_cmap('viridis'))
16+
17+
18+
mcm.unregister_cmap('viridis'[::-1])
19+
with pytest.raises(ValueError):
20+
mcm.get_cmap('viridis'[::-1])
21+
# test that second time is error free
22+
mcm.unregister_cmap('viridis'[::-1])
23+
24+
25+
def test_unregister_cmap():
26+
with pytest.raises(ValueError):
27+
mcm.unregister_cmap('viridis')

0 commit comments

Comments
 (0)