Skip to content

Implemented __copy__ in Colormap #8314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down
27 changes: 27 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import copy
import six
import itertools
import warnings
Expand Down Expand Up @@ -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
Expand Down