Skip to content

FIX: Allow different colormap name from registered name #25479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/matplotlib/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ def register(self, cmap, *, name=None, force=False):
"that was already in the registry.")

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

def unregister(self, name):
"""
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ def __copy__(self):
return cmapobject

def __eq__(self, other):
if (not isinstance(other, Colormap) or self.name != other.name or
if (not isinstance(other, Colormap) or
self.colorbar_extend != other.colorbar_extend):
return False
# To compare lookup tables the Colormaps have to be initialized
Expand Down
16 changes: 14 additions & 2 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ def test_colormap_equals():
# Make sure we can compare different sizes without failure
cm_copy._lut = cm_copy._lut[:10, :]
assert cm_copy != cmap
# Test different names are not equal
# Test different names are equal if the lookup table is the same
cm_copy = cmap.copy()
cm_copy.name = "Test"
assert cm_copy != cmap
assert cm_copy == cmap
# Test colorbar extends
cm_copy = cmap.copy()
cm_copy.colorbar_extend = not cmap.colorbar_extend
Expand Down Expand Up @@ -1649,3 +1649,15 @@ def test_cm_set_cmap_error():
bad_cmap = 'AardvarksAreAwkward'
with pytest.raises(ValueError, match=bad_cmap):
sm.set_cmap(bad_cmap)


def test_set_cmap_mismatched_name():
cmap = matplotlib.colormaps["viridis"].with_extremes(over='r')
# register it with different names
cmap.name = "test-cmap"
matplotlib.colormaps.register(name='wrong-cmap', cmap=cmap)

plt.set_cmap("wrong-cmap")
cmap_returned = plt.get_cmap("wrong-cmap")
assert cmap_returned == cmap
assert cmap_returned.name == "wrong-cmap"