You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ENH: Switch to constrained layout in figure creation #12024
I've mentioned this a few times in passing (e.g., 1, 2) but thought it deserves its own issue. I've been using plt.subplots(..., constrained_layout=True) for a few years in many use cases and I can't think of a time it produced a bad (or even sub-optimal) result. By comparison, the default tight_layout engine and workflow almost always needs some manual adjustment to use space optimally.
I propose we try switching our plt.subplots and related matplotlib-figure-creation calls to use the constrained layout engine wherever possible, build the doc, and see if it does indeed improve things. In particular I expect it:
Will produce better results more consistently in (nearly?) all cases compared to current code that calls subplots_adjust and tight_layout
Is easy enough for people to get old behavior back by switching the layout engine on their own with fig.set_layout_engine('tight') and/or fig.tight_layout etc.
Will allow us to simplify our code because we won't need to tweak plot margins etc. nearly as much (or at all) anymore, just make minimal decisions (e.g., how much space do I want to reserve for the colorbar?)
By removing all our internal uses of subplots_adjust / tight_layout, if a user passes in their own axes we'll leave them alone rather than tweak the figure properties, which seems like cleaner/nicer behavior. (If a user passes axes it should be up to them to deal with spacing issues however they wish without us messing with the spacing.)
So I expect an overall meaningful aesthetic and maintenance win by going this route. Since we consider aesthetic changes non-breaking I don't think we need a deprecation cycle for this. If we anticipate user backlash, we could add a mne.viz.use/get/set_figure_layout_engine, but I'd assume YAGNI and just have users do fig.set_layout_engine('tight') when they need it.
Bringing this up was inspired by @mscheltienne going this direction a bit already in #12020.
Reacted by Mathieu Scheltienne, Daniel McCloy and Mikolaj Magnuski
Activity
larsoner commentedon Sep 28, 2023
@mscheltienne maybe one for you for the sprint if this ends up being uncontroversial and you're motivated :)
hoechenberger commentedon Sep 28, 2023
Let's do this