Skip to content

Commit 981f273

Browse files
committed
Issue matplotlib#8299, implemented copy, added test
1 parent 16b4ce4 commit 981f273

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎lib/matplotlib/colors.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,17 @@ def __call__(self, X, alpha=None, bytes=False):
530530
rgba = tuple(rgba[0, :])
531531
return rgba
532532

533+
def __copy__(self):
534+
"""Create new object with the same class and update attributes
535+
If object is initialized, copy the elements of _lut list
536+
"""
537+
cls = self.__class__
538+
newCMapObj = cls.__new__(cls)
539+
newCMapObj.__dict__.update(self.__dict__)
540+
if self._isinit:
541+
newCMapObj._lut = np.copy(self._lut)
542+
return newCMapObj
543+
533544
def set_bad(self, color='k', alpha=None):
534545
"""Set color to be used for masked values.
535546
"""

‎lib/matplotlib/tests/test_colors.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import (absolute_import, division, print_function,
22
unicode_literals)
33

4+
import copy
45
import six
56
import itertools
67
import warnings
@@ -44,6 +45,32 @@ def test_resample():
4445
assert_array_almost_equal(lc3([0, 0.5, 1]), expected)
4546

4647

48+
def test_colormap_copy():
49+
cm = plt.cm.Reds
50+
cm([-1, 0, .5, np.nan, np.inf])
51+
cm.set_bad('y')
52+
cm2 = copy.copy(cm)
53+
cm2.set_bad('b')
54+
cm.set_under('k')
55+
cm2.set_under('r')
56+
cm.set_over('c')
57+
cm2.set_over('m')
58+
59+
expected1 = plt.cm.Reds
60+
expected2 = plt.cm.Reds
61+
expected1([-1, 0, .5, np.nan, np.inf])
62+
expected2([-1, 0, .5, np.nan, np.inf])
63+
expected1.set_bad('y')
64+
expected2.set_bad('b')
65+
expected1.set_under('k')
66+
expected2.set_under('r')
67+
expected1.set_over('c')
68+
expected2.set_over('m')
69+
70+
assert_array_equal(cm._lut, expected1._lut)
71+
assert_array_equal(cm2._lut, expected2._lut)
72+
73+
4774
def test_colormap_endian():
4875
"""
4976
Github issue #1005: a bug in putmask caused erroneous

0 commit comments

Comments
 (0)