matplotlib / cheatsheets

Official Matplotlib cheat sheets

Home Page:https://matplotlib.org/cheatsheets/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`tight_layout` versus `constrained_layout`

jklymak opened this issue · comments

Once your figure is finished, you can call {\ttfamily tight\_layout()}
calls out tight_layout in particular. Is there a reason to not also call out constrained_layout? In general constrained_layout is more "automatic".

The reason is that I did not know about constrained_layout... What's the difference with tight_layout?

Simply speaking: Constrained layout does a better layouting job in many cases.

From constrained layout guide:

constrained_layout is similar to tight_layout, but uses a constraint solver to determine the size of axes that allows them to fit.

But that's only the technical implementation. @jklymak I don't think we have an explanation what that means for the user. Would be nice to add a sentence to the constrained layout guide as well.

constrained layout works with multi-axes colorbars, it makes axes match size whether they have colorbars or not, it works with nested gridspecs. It works with uneven subplot layouts (i.e. 2 axes in one column, and 3 axes in another column). It also works interactively - i.e. when the axes is resized, which I don't think tight_layout does. Overall, I think most users want constrained_layout versus tight_layout - I'm not aware of situations where tight_layout is better, but happy for reports.

And is possible to remove white margins in the process? (like pdfcrop does)

Sure if you set hpad and wpad to zero.