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

Skip to content

Commit f94d64d

Browse files
committed
MAINT: Adding an equals method to colormaps
This adds an __eq__ method to Colormap to test the lookup tables for equality.
1 parent cb21d7d commit f94d64d

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/matplotlib/colors.py

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

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

lib/matplotlib/tests/test_colors.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,21 @@ 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+
179+
165180
def test_colormap_endian():
166181
"""
167182
GitHub issue #1005: a bug in putmask caused erroneous

0 commit comments

Comments
 (0)