8000 Merge pull request #8375 from vidursatija/i_8299 · matplotlib/matplotlib@d50843e · GitHub
[go: up one dir, main page]

Skip to content

Commit d50843e

Browse files
authored
Merge pull request #8375 from vidursatija/i_8299
FIX: implemented copy on color map objects
2 parents 8ae0dbf + 1579364 commit d50843e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/matplotlib/colors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,16 @@ 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, update attributes
535+
"""
536+
cls = self.__class__
537+
cmapobject = cls.__new__(cls)
538+
cmapobject.__dict__.update(self.__dict__)
539+
if self._isinit:
540+
cmapobject._lut = np.copy(self._lut)
541+
return cmapobject
542+
533543
def set_bad(self, color='k', alpha=None):
534544
"""Set color to be used for masked values.
535545
"""

lib/matplotlib/tests/test_colors.py

Lines changed: 13 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
@@ -43,6 +44,18 @@ def test_resample():
4344
assert_array_almost_equal(lc3([0, 0.5, 1]), expected)
4445

4546

47+
def test_colormap_copy():
48+
cm = plt.cm.Reds
49+
cm_copy = copy.copy(cm)
50+
with np.errstate(invalid='ignore'):
51+
ret1 = cm_copy([-1, 0, .5, 1, np.nan, np.inf])
52+
cm2 = copy.copy(cm_copy)
53+
cm2.set_bad('g')
54+
with np.errstate(invalid='ignore'):
55+
ret2 = cm_copy([-1, 0, .5, 1, np.nan, np.inf])
56+
assert_array_equal(ret1, ret2)
57+
58+
4659
def test_colormap_endian():
4760
"""
4861
Github issue #1005: a bug in putmask caused erroneous

0 commit comments

Comments
 (0)
0