8000 FIX: Update the colormap object's name when registering · matplotlib/matplotlib@e327961 · GitHub
[go: up one dir, main page]

Skip to content

Commit e327961

Browse files
committed
FIX: Update the colormap object's name when registering
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, updating the copy of the object that gets registered. Additionally, the equals method of Colormaps shouldn't care about the name of the object, we should only worry about whether the values are the same in the lookup tables and let someone use object identity if they are worried about the name of the object.
1 parent 1d1d8ba commit e327961

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
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: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,6 @@ 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
199-
cm_copy = cmap.copy()
200-
cm_copy.name = "Test"
201-
assert cm_copy != cmap
202198
# Test colorbar extends
203199
cm_copy = cmap.copy()
204200
cm_copy.colorbar_extend = not cmap.colorbar_extend
@@ -1649,3 +1645,15 @@ def test_cm_set_cmap_error():
16491645
bad_cmap = 'AardvarksAreAwkward'
16501646
with pytest.raises(ValueError, match=bad_cmap):
16511647
sm.set_cmap(bad_cmap)
1648+
1649+
1650+
def test_set_cmap_mismatched_name():
1651+
cmap = matplotlib.colormaps["viridis"].with_extremes(over='r')
1652+
# register it with different names
1653+
cmap.name = "test-cmap"
1654+
matplotlib.colormaps.register(name='wrong-cmap', cmap=cmap)
1655+
1656+
plt.set_cmap("wrong-cmap")
1657+
cmap_returned = plt.get_cmap("wrong-cmap")
1658+
assert cmap_returned == cmap
1659+
assert cmap_returned.name == "wrong-cmap"

0 commit comments

Comments
 (0)
0