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

Skip to content

Commit 63c97a2

Browse files
authored
Merge pull request #25479 from greglucas/cmap-bad-name
FIX: Allow different colormap name from registered name
2 parents 139c045 + cc977dc commit 63c97a2

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

lib/matplotlib/cm.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ def register(self, cmap, *, name=None, force=False):
146146
"that was already in the registry.")
147147

148148
self._cmaps[name] = cmap.copy()
149+
# Someone may set the extremes of a builtin colormap and want to register it
150+
# with a different name for future lookups. The object would still have the
151+
# builtin name, so we should update it to the registered name
152+
if self._cmaps[name].name != name:
153+
self._cmaps[name].name = name
149154

150155
def unregister(self, name):
151156
"""

lib/matplotlib/colors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ def __copy__(self):
774774
return cmapobject
775775

776776
def __eq__(self, other):
777-
if (not isinstance(other, Colormap) or self.name != other.name or
777+
if (not isinstance(other, Colormap) or
778778
self.colorbar_extend != other.colorbar_extend):
779779
return False
780780
# To compare lookup tables the Colormaps have to be initialized

lib/matplotlib/tests/test_colors.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ def test_colormap_equals():
195195
# Make sure we can compare different sizes without failure
196196
cm_copy._lut = cm_copy._lut[:10, :]
197197
assert cm_copy != cmap
198-
# Test different names are not equal
198+
# Test different names are equal if the lookup table is the same
199199
cm_copy = cmap.copy()
200200
cm_copy.name = "Test"
201-
assert cm_copy != cmap
201+
assert cm_copy == cmap
202202
# Test colorbar extends
203203
cm_copy = cmap.copy()
204204
cm_copy.colorbar_extend = not cmap.colorbar_extend
@@ -1649,3 +1649,15 @@ def test_cm_set_cmap_error():
16491649
bad_cmap = 'AardvarksAreAwkward'
16501650
with pytest.raises(ValueError, match=bad_cmap):
16511651
sm.set_cmap(bad_cmap)
1652+
1653+
1654+
def test_set_cmap_mismatched_name():
1655+
cmap = matplotlib.colormaps["viridis"].with_extremes(over='r')
1656+
# register it with different names
1657+
cmap.name = "test-cmap"
1658+
matplotlib.colormaps.register(name='wrong-cmap', cmap=cmap)
1659+
1660+
plt.set_cmap("wrong-cmap")
1661+
cmap_returned = plt.get_cmap("wrong-cmap")
1662+
assert cmap_returned == cmap
1663+
assert cmap_returned.name == "wrong-cmap"

0 commit comments

Comments
 (0)