From cc977dcc6341f8b146985dff9737a46e6203c587 Mon Sep 17 00:00:00 2001 From: Greg Lucas Date: Mon, 24 Apr 2023 20:19:56 -0600 Subject: [PATCH] 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. --- lib/matplotlib/cm.py | 5 +++++ lib/matplotlib/colors.py | 2 +- lib/matplotlib/tests/test_colors.py | 16 ++++++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/cm.py b/lib/matplotlib/cm.py index 38158db9564a..78c4ce163579 100644 --- a/lib/matplotlib/cm.py +++ b/lib/matplotlib/cm.py @@ -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): """ diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 05922c93dfda..434bb5423543 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -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 diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 2e841699cdd4..4f441f8a05a9 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -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 @@ -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"