Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4fbc57d

Browse 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

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/matplotlib/pyplot.py

Lines 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

Lines 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)