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

Matplotlib doesn't save correctly the figuren when using patches.Circle on different plots #1148

Closed
NelleV opened this issue Aug 27, 2012 · 10 comments

Comments

@NelleV
Copy link
Member

NelleV commented Aug 27, 2012

Matplotlib doesn't save the figure as it shows it: the third figure's circle is not rendered the same way (different size and center) than the one displayed with plt.show()

The code to reproduce the bug can be found here:
https://gist.github.com/3488488

The data files (numpy arrays) to reproduce the code can be found here: http://cbio.ensmp.fr/~nvaroquaux/matplotlib_bg.tar

I am using matplotlib version 1.1.0, with python2.7

@pelson
Copy link
Member

pelson commented Aug 27, 2012

It would really have helped if you could have provided a SSCCE.

In this case, I think you are using the same collection on more than one axes instance which is not supported in matplotlib.

@NelleV
Copy link
Member Author

NelleV commented Aug 27, 2012

@pelson I provided code and data files in the tarball to reproduce the issue. I did not have time to make a smaller self contained example, nor to investigate the issue in depth.

The fact that a collection can one be used on one axes instance doesn't seem to be mentioned in the documentation nor in the docstring (I can't find it at least). If not supported, it should IMO at least be mentioned in the docstring. I personnally think that this is a bug. It is not clear why, from a user point of view, we would not be able to reuse such an object.

@WeatherGod
Copy link
Member

Every artist is attached to a single axes. This is part and parcel how the
artist hierarchy works. A collection object is a subclass of artist,
therefore, it can only be attached to a single axes. If you are using
ax.add_collection(), I believe it automatically disconnects the
collection's current axes and connects it to the latest axes (but I could
be wrong here).

If this isn't in the Artist documentation, then it probably could be a bit
clearer.

@pelson
Copy link
Member

pelson commented Aug 27, 2012

I agree.

How about the following to reproduce:


import matplotlib.pyplot as plt

fig1 = plt.figure(figsize=(6, 6))
fig2 = plt.figure(figsize=(4, 6))

ax1 = fig1.add_subplot(111)
ax2 = fig2.add_subplot(111)


import matplotlib.patches as mpatches
import matplotlib.collections as mcollections

patch = mpatches.Circle([0.5, 0.5], radius=0.25)
collection = mcollections.PatchCollection([patch])

ax1.add_collection(collection)
ax2.add_collection(collection)

plt.show()

(you get some nasty clipping of the circle in figure 1 as a result of the underlying collection's transform being implicitly bound to axes 2.

Workaround: Make two collections from the same set of patches:

collection1 = mcollections.PatchCollection([patch])
collection2 = mcollections.PatchCollection([patch])


ax1.add_collection(collection1)
ax2.add_collection(collection2)

I cannot see that "fixing" this is possible, but I do agree that there is a documentation issue here.

@NelleV
Copy link
Member Author

NelleV commented Aug 27, 2012

my bug is not exactly that. When running it in ipython (or python), it displays the plots properly. It just doesn't save them properly. Also the figures are of the same size and shape. I'll checkout how to do a smaller script to reproduce the bug if I have time tomorrow.

@pelson
Copy link
Member

pelson commented Aug 27, 2012

Does the workaround solve the issue for you though? That would be the give-away if it does.

@NelleV
Copy link
Member Author

NelleV commented Aug 27, 2012

@pelson recreating everything works, indeed.

@NelleV
Copy link
Member Author

NelleV commented Aug 27, 2012

Here's a new gist with the bug report: https://gist.github.com/3491953
The image displayed when running python bug_report.py do not match the images im1.png and im2.png generated with the script.

@pelson
Copy link
Member

pelson commented Aug 27, 2012

I agree what is rendered is not what is drawn. This is fairly clearly related to the fact that the collection is being used in two different axes (and therefore the collection's transform is only representative of one of the figure's "device" coordinates). I can't pinpoint what exactly is causing the funny rendering in one case and not the other, but as I said, my feeling is that this is a documentation issue and it cannot be resolved fully without a major re-work. Since this falls slightly under the transform radar (one artist can only have one full stack transform) I would be interested to hear what @mdboom has to say.

Thanks for reporting this @NelleV and spending the time getting a SSCCE together.

@tacaswell
Copy link
Member

Closing this because as @WeatherGod says, artists can only be in one axes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants