-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Repeated calls to tight_layout needed with aspect='equal' #18313
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
Comments
You are asking for two constraints that can't be solved at the same time: 1) that the figure is a certain size and the axes fill the figure, 2) that the axes be a fixed aspect ratio. Given that you have set the aspect ratio to equal, there has to be extra space unless your figure size is tuned to match. So where to put that space? So yes this would be nice to fix, but unfortunately its not easy. You may want to look at image_grid (https://matplotlib.org/gallery/axes_grid1/simple_axesgrid.html) which makes a ready-made set of axes that do this automatically, but I don't think they will obey tight_layout. |
Thanks! Yeah, I understand that this is a really hard thing to implement. Any other way to achieve it? My usecase for this is: I need my figures to span exactly the text width of an A4 paper size, which typically is either 16cm or 17cm, while the vertical spacing should be minimized as much as possible. Is there any way to define the figure width while leaving the height flexible?
|
I'd start by making the aspect ratio of the figure closer to one-to-one (for a 2x2 grid of axes). That won't be perfect depending on tick labels and other labels, but will be closer. After that you can adjust by hand. When #17246 gets completed, the idea will be to call that, and then call |
Yep, this was a fairly extreme example to show what I wanted to show. :) |
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! |
I think this seems to work alright with the compressed layout engine (pass |
Should work. Lets close. |
Bug report
Producing 4 subplots with a rectangular figure (height > width) and setting the subplots to
ax.set_aspect('equal')
causesfig.tight_layout(pad=0., h_pad=0., w_pad=0.)
to introduce a huge vertical gap between the subplots on the first call.Each subsequent call to
tight_layout
reduces the gap slowly, until the set argumentspad
andh_pad
are satisfied.Code for reproduction
To reduce the vertical gap until padding is satisfied:
And it seems that for plots with a high complexity, calls to
fig.canvas.draw
are required between the calls totight_layout
. But I cannot reproduce this reliably:Actual outcome

The outcome of the code without repeated calls to tight_layout:
Expected outcome
The in my opinion correct output can be produced with:
Expected output:

Matplotlib version
All packages are installed from the default conda channel.
The text was updated successfully, but these errors were encountered: