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

Accessing default norm of a Collection removes its colors. #13887

Closed
ImportanceOfBeingErnest opened this issue Apr 4, 2019 · 5 comments · Fixed by #27347
Closed

Accessing default norm of a Collection removes its colors. #13887

ImportanceOfBeingErnest opened this issue Apr 4, 2019 · 5 comments · Fixed by #27347
Milestone

Comments

@ImportanceOfBeingErnest
Copy link
Member

ImportanceOfBeingErnest commented Apr 4, 2019

This gave me some headaches to even isolate. Very weird bug!

Bug report

Bug summary

Accessing the default norm of a Collection removes its colors.

Code for reproduction

Create a LineCollection and access its norm.

import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

lines = LineCollection(np.random.rand(4,3,2), array=np.arange(4))  
plt.gca().add_collection(lines)

print(lines.norm(2))

plt.show()

Actual outcome

image

>>> 0.0

Same bug is present if you replace LineCollection by PolyCollection. Possibly other collections are equally affected.

Expected outcome

Expected outcome is the same as when removing the print statement.

lines = LineCollection(np.random.rand(4,3,2), array=np.arange(4))  
plt.gca().add_collection(lines)

plt.show()

image

Fun Fact:

If adding a colorbar first, the whole thing runs fine.

lines = LineCollection(np.random.rand(4,3,2), array=np.arange(4))  
plt.gca().add_collection(lines)

plt.colorbar(lines)
print(lines.norm(2))

plt.show()

image

>>> 0.6666666666666666

Matplotlib version

  • Operating system: Win 8.1
  • Matplotlib version: 1.5.3 (!!!) to current master
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg (1.5.3), Qt5Agg (master)
  • Python version: 3.6
@ImportanceOfBeingErnest ImportanceOfBeingErnest added this to the v3.1.1 milestone Apr 4, 2019
@ImportanceOfBeingErnest ImportanceOfBeingErnest changed the title Accessing default norm of a LineCollection removes its colors. Accessing default norm of a Collection removes its colors. Apr 4, 2019
@anntzer
Copy link
Contributor

anntzer commented Apr 5, 2019

This happens because the norm bounds (min/max) are only computed at draw time, but when you call norm(2) you trigger a first calculation of bounds using 2 as sole input and then the Normalize instance stays stuck with vmin=vmax=2.

One could change it to have the min/max be computed when the LineCollection is instantiated instead, but I'm sure someone out there relies on being able to change the Collection's mapped values after instantiation but before drawing and then expects the min/max to pick it up. Having thought approximately 15s about it, I do think that normalizing at artist instantiation (specifically, in set_norm) does look better than at first draw, though (and the above-mentioned disgruntled users can always call set_norm() with a new Normalize instance after updating the mapped values -- which will work even after the first draw.

Remilestoning as 3.2 as I think it's a change in "intended" behavior (the degree of intent is unclear in this case, though :-)).

@anntzer anntzer modified the milestones: v3.1.1, v3.2.0 Apr 5, 2019
@ImportanceOfBeingErnest
Copy link
Member Author

I understand that any fix to this might be an API change. My expectation simply comes from the fact that

im = plt.imshow(np.random.normal(size=(100,100)))
print(im.norm(2))

or

sc = plt.scatter([1,2,3],[3,4,5], c=[0,4,1])
print(sc.norm(2))

work as expected.
My thinking is that this should be a matter of keeping track of whether the norm has been None or user-specified.

@tacaswell tacaswell modified the milestones: v3.2.0, needs sorting Sep 10, 2019
@github-actions
Copy link

This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Jun 12, 2023
@jklymak
Copy link
Member

jklymak commented Jun 12, 2023

I didn't check if this was still an issue but if so seems worth fixing.

@ksunden
Copy link
Member

ksunden commented Jun 12, 2023

Can confirm that the behavior still exists.

@jklymak jklymak added keep Items to be ignored by the “Stale” Github Action and removed status: inactive Marked by the “Stale” Github Action labels Jun 12, 2023
@QuLogic QuLogic modified the milestones: future releases, v3.9.0 Nov 28, 2023
@QuLogic QuLogic removed the keep Items to be ignored by the “Stale” Github Action label Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants