Skip to content

auto legend position changes upon saving the figure #7353

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
anntzer opened this issue Oct 25, 2016 · 2 comments
Closed

auto legend position changes upon saving the figure #7353

anntzer opened this issue Oct 25, 2016 · 2 comments
Labels
Milestone

Comments

@anntzer
Copy link
Contributor

anntzer commented Oct 25, 2016

2.0b4 and master

from matplotlib import pyplot as plt
import numpy as np

plt.rcdefaults()
np.random.seed(1)

plt.plot(*np.random.rand(2, 300), ".", label="some long legend")
plt.plot(*np.random.rand(2, 300), ".", label="other long legend")
plt.legend()
plt.savefig("/tmp/foo.png")
plt.show()

The displayed window puts the legend at the center top of the figure, but the saved image puts the legend at the bottom left.

By changing the seed one can get cases where the two legends do match each other. In that case, careful inspection of the images show that the legends have slightly different sizes, which make them overlap with different number of points depending on the corner in which they are tentatively placed by the autolocator -- this is what leads to them moving upon saving.

@tacaswell
Copy link
Member

The first time it saves:

foo

Saving after letting it render to the screen

foo2

And they are slightly different sizes

In [63]: mimg.imread('/tmp/foo.png').shape
Out[63]: 
(480, 640, 4)

In [64]: mimg.imread('/tmp/foo2.png').shape
Out[64]: 
(471, 640, 4)

Which leads to the even weirder observation that fig.set_size_inches does not always take and some times comes back a bit shorter, which is in turn tied to forward=True as the new default. If you set the same size twice it takes.

In [108]: plt.gcf().set_size_inches([6.4, 4.7], forward=True); print(plt.gcf().bbox_inches); print(plt.gcf().get_size_inches())
Bbox(x0=0.0, y0=0.0, x1=6.4, y1=4.61)
[ 6.4   4.61]

In [109]: plt.gcf().set_size_inches([6.4, 4.7], forward=True); print(plt.gcf().bbox_inches); print(plt.gcf().get_size_inches())
Bbox(x0=0.0, y0=0.0, x1=6.4, y1=4.7)
[ 6.4  4.7]

This seems to be related to something going wrong with the conversion between the size of the canvas and the size of the qt window.

@tacaswell tacaswell added this to the 2.0.1 (next bug fix release) milestone Oct 26, 2016
QuLogic added a commit to QuLogic/matplotlib that referenced this issue Dec 11, 2016
Even though the minimum height is set, that doesn't seem to carry over
to the size hint that gets used later to determine the size of the
entire window, causing the window to be a bit too short and changing the
size of the canvas.

Fixes matplotlib#7353. Probably also fixes matplotlib#7472.
QuLogic added a commit to QuLogic/matplotlib that referenced this issue Dec 11, 2016
Even though the minimum height is set, that doesn't seem to carry over
to the size hint that gets used later to determine the size of the
entire window, causing the window to be a bit too short and changing the
size of the canvas.

Fixes matplotlib#7353. Probably also fixes matplotlib#7472.
@tacaswell tacaswell modified the milestones: 2.0 (style change major release), 2.0.1 (next bug fix release) Dec 17, 2016
@tacaswell
Copy link
Member

closed by #7610

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

No branches or pull requests

2 participants