Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 62403dc

Browse filesBrowse files
committed
ENH: add method to de-register a color map
1 parent 8167da6 commit 62403dc
Copy full SHA for 62403dc

File tree

Expand file treeCollapse file tree

3 files changed

+40
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

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

‎lib/matplotlib/cm.py

Copy file name to clipboardExpand all lines: lib/matplotlib/cm.py
+22Lines 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 deregister_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 deregister {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

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_cm.py
+12Lines 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.deregister_cmap('viridis'[::-1])
19+
with pytest.raises(ValueError):
20+
mcm.get_cmap('viridis'[::-1])
21+
# test that second time is error free
22+
mcm.deregister_cmap('viridis'[::-1])
23+
24+
25+
def test_deregister_cmap():
26+
with pytest.raises(ValueError):
27+
mcm.deregister_cmap('viridis')

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.