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 4fbc57d

Browse filesBrowse files
committed
FIX: Allow different colormap name from registered name
When registering a colormap you can use a different name than the "cmap.name" attribute now. This will set the colormap based on the registered name rather than cmap.name
1 parent 63b9b04 commit 4fbc57d
Copy full SHA for 4fbc57d

File tree

Expand file treeCollapse file tree

2 files changed

+19
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+19
-2
lines changed

‎lib/matplotlib/pyplot.py

Copy file name to clipboardExpand all lines: lib/matplotlib/pyplot.py
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,9 +2180,13 @@ def set_cmap(cmap):
21802180
matplotlib.cm.register_cmap
21812181
matplotlib.cm.get_cmap
21822182
"""
2183+
# The name of the colormap may be different than the registered name.
2184+
# We want the registered name for the rc setting
2185+
name = getattr(cmap, "name", cmap)
2186+
# Run this through get_cmap to confirm this is a valid cmap before
2187+
# updating the rc parameters
21832188
cmap = get_cmap(cmap)
2184-
2185-
rc('image', cmap=cmap.name)
2189+
rc('image', cmap=name)
21862190
im = gci()
21872191

21882192
if im is not None:

‎lib/matplotlib/tests/test_colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_colors.py
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,3 +1603,16 @@ def test_cm_set_cmap_error():
16031603
bad_cmap = 'AardvarksAreAwkward'
16041604
with pytest.raises(ValueError, match=bad_cmap):
16051605
sm.set_cmap(bad_cmap)
1606+
1607+
1608+
def test_set_cmap_mismatched_name():
1609+
cmap = matplotlib.colormaps["viridis"].copy()
1610+
# register it with different names
1611+
cmap.name = "test-cmap"
1612+
matplotlib.colormaps.register(name='wrong-cmap', cmap=cmap)
1613+
1614+
plt.set_cmap("wrong-cmap")
1615+
cmap_returned = plt.get_cmap("wrong-cmap")
1616+
assert cmap_returned == cmap
1617+
# The name of the cmap is test-cmap even though we requested wrong-cmap
1618+
assert cmap_returned.name == "test-cmap"

0 commit comments

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