diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 8b0e8350d6af..627d8b96c54e 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -529,7 +529,18 @@ def __call__(self, X, alpha=None, bytes=False): if vtype == 'scalar': rgba = tuple(rgba[0, :]) return rgba - + + def __copy__(self): + """Create new object with the same class and update attributes + If object is initialized, copy the elements of _lut list + """ + cls = self.__class__ + newCMapObj = cls.__new__(cls) + newCMapObj.__dict__.update(self.__dict__) + if self._isinit: + newCMapObj._lut = np.copy(self._lut) + return newCMapObj + def set_bad(self, color='k', alpha=None): """Set color to be used for masked values. """ diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 0d1243c94552..5cfc5a2a3c42 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -1,6 +1,7 @@ from __future__ import (absolute_import, division, print_function, unicode_literals) +import copy import six import itertools import warnings @@ -44,6 +45,32 @@ def test_resample(): assert_array_almost_equal(lc3([0, 0.5, 1]), expected) +def test_colormap_copy(): + cm = plt.cm.Reds + cm([-1, 0, .5, np.nan, np.inf]) + cm.set_bad('y') + cm2 = copy.copy(cm) + cm2.set_bad('b') + cm.set_under('k') + cm2.set_under('r') + cm.set_over('c') + cm2.set_over('m') + + expected1 = plt.cm.Reds + expected2 = plt.cm.Reds + expected1([-1, 0, .5, np.nan, np.inf]) + expected2([-1, 0, .5, np.nan, np.inf]) + expected1.set_bad('y') + expected2.set_bad('b') + expected1.set_under('k') + expected2.set_under('r') + expected1.set_over('c') + expected2.set_over('m') + + assert_array_equal(cm._lut, expected1._lut) + assert_array_equal(cm2._lut, expected2._lut) + + def test_colormap_endian(): """ Github issue #1005: a bug in putmask caused erroneous