Skip to content
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

copy a color map object does not isolate changes to cm #8299

Closed
tacaswell opened this issue Mar 15, 2017 · 3 comments
Closed

copy a color map object does not isolate changes to cm #8299

tacaswell opened this issue Mar 15, 2017 · 3 comments
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
Milestone

Comments

@tacaswell
Copy link
Member

If you do cm2 = copy.copy(cm) and the original color map has been used at least once, then the copy will share a reference to self._lut (which is what is used to __call__ to map from values -> rgba). Any calls to set_over, set_under, or set_bad will be shared by all copies (as they update self._lut in place).

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Mar 15, 2017
@tacaswell tacaswell added new-contributor-friendly Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues labels Mar 15, 2017
@tacaswell
Copy link
Member Author

Exact work:

  • implement __copy__ and maybe __deepcopy__ on ColorMap

Tagging as medium because the color map code is a bit convoluted .

@bendichter
Copy link

I can reproduce this issue with:

import matplotlib.pyplot as plt
import copy
import numpy as np

%matplotlib inline
data = np.ma.masked_array([[1,2,3],[2,3,4]], mask=[[1,0,0],[0,0,0]])
cm = plt.cm.Reds
plt.imshow(data,cmap=cm)
plt.show()
cm2 = copy.copy(cm)
cm2.set_bad('b')
plt.imshow(data,cmap=cm)

In the second plot the image contains blue when it should remain white. copy.deepcopy works as expected, though. Would the solution be to have copy call deepcopy?

@dopplershift
Copy link
Contributor

Implementing __copy__ should be enough to get the desired behavior.

vidursatija added a commit to vidursatija/matplotlib that referenced this issue Mar 24, 2017
vidursatija added a commit to vidursatija/matplotlib that referenced this issue May 14, 2017
Implemented with no leak into Reds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
Projects
None yet
Development

No branches or pull requests

3 participants