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

Skip to content

Commit 136dc16

Browse files
authored
Merge pull request #20227 from greglucas/cm-equals
ENH: Adding an equals method to colormaps
2 parents 772e382 + b99c620 commit 136dc16

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/matplotlib/colors.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,17 @@ def __copy__(self):
690690
cmapobject._global = False
691691
return cmapobject
692692

693+
def __eq__(self, other):
694+
if (not isinstance(other, Colormap) or self.name != other.name or
695+
self.colorbar_extend != other.colorbar_extend):
696+
return False
697+
# To compare lookup tables the Colormaps have to be initialized
698+
if not self._isinit:
699+
self._init()
700+
if not other._isinit:
701+
other._init()
702+
return np.array_equal(self._lut, other._lut)
703+
693704
def get_bad(self):
694705
"""Get the color for masked values."""
695706
if not self._isinit:

lib/matplotlib/tests/test_colors.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,29 @@ def test_colormap_copy():
162162
assert_array_equal(ret1, ret2)
163163

164164

165+
def test_colormap_equals():
166+
cmap = plt.get_cmap("plasma")
167+
cm_copy = cmap.copy()
168+
# different object id's
169+
assert cm_copy is not cmap
170+
# But the same data should be equal
171+
assert cm_copy == cmap
172+
# Change the copy
173+
cm_copy.set_bad('y')
174+
assert cm_copy != cmap
175+
# Make sure we can compare different sizes without failure
176+
cm_copy._lut = cm_copy._lut[:10, :]
177+
assert cm_copy != cmap
178+
# Test different names are not equal
179+
cm_copy = cmap.copy()
180+
cm_copy.name = "Test"
181+
assert cm_copy != cmap
182+
# Test colorbar extends
183+
cm_copy = cmap.copy()
184+
cm_copy.colorbar_extend = not cmap.colorbar_extend
185+
assert cm_copy != cmap
186+
187+
165188
def test_colormap_endian():
166189
"""
167190
GitHub issue #1005: a bug in putmask caused erroneous

0 commit comments

Comments
 (0)