Skip to content

Should constrained_layout be applied every draw? #11630

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
jklymak opened this issue Jul 11, 2018 · 6 comments
Closed

Should constrained_layout be applied every draw? #11630

jklymak opened this issue Jul 11, 2018 · 6 comments
Labels
topic: geometry manager LayoutEngine, Constrained layout, Tight layout

Comments

@jklymak
Copy link
Member

jklymak commented Jul 11, 2018

Discussion

Right now fig, ax = plt.subplots(constrained_layout=True) will cause the axes resizing to be carried out every draw event; hence the axes get repositioned if the figure changes size, or if zoom is applied, or panning, etc.

Is this desirable?

plt.tight_layout() is only called once, and does not get activated at draw time.

Pros:

  • if the window is resized, the axes positions are recalculated. That means the padding and margins expand or contract as the window gets smaller or larger.
  • if we zoom, and the ticklabels get much longer (i.e. more decimal places), the axes get resized.

Cons:

  • under zoom the axes keep changing size. That can be distracting. It doesn't always work either because there is a strange interaction with zoom and clipping boxes for artists.
  • for a plot w/ lots of axes it makes zooming/panning less responsive.

Any opinions? For my usual workflow, I'm probably fine with just calling constrained layout on the original plot. Any zooming etc are done on working plots versus final presentation plots, and I hard code limits in after the exploration stage. But I get the feeling some people use a lot of GUI work before saving their plots.

@jklymak jklymak added the topic: geometry manager LayoutEngine, Constrained layout, Tight layout label Jul 11, 2018
@timhoffm
Copy link
Member

It may be worth making the behavior configurable. As you said, there are use-cases for both scenarios.

@ImportanceOfBeingErnest
Copy link
Member

I have a crazy idea: tight_layout can be called from within the GUI

image

Would it make sense to add another button for constrained_layout? This would be a toggle button - as long as it is active constrained_layout will take effect, but you may toggle it off as well. Not sure what the default state would be though.

@ImportanceOfBeingErnest
Copy link
Member

Would it affect animations? Like for each frame that is shown contrained_layout is called? I think that would be completely undesired.

@jklymak
Copy link
Member Author

jklymak commented Jul 11, 2018

@ImportanceOfBeingErnest and @timhoffm Thanks for your thoughts...

First, constrained_layout needs to be turned on when the figure is made - it needs more of a tree heirarchy than Matplotlib provides by itself. So a simple GUI toggle doesn't suffice, because you can't go back and remake the figure.

Programatically, we can turn off constrained_layout at any time with fig.do_constrained_layout(False). Setting it to True will turn it back on.

We could have an rcParam['constrained_layout.firstdraw_only']. I imagine we could also extend the above so that we could call fig.do_constrained_layout('firstdraw_only') or fig.do_constrained_layout(True, firstdraw_only=True).

@afvincent was talking about the layout GUI in another thread. I think it'd make sense for the GUI to be different if constrained_layout is on for the figure, allow the constrained_layout margin and pad parameters to be fiddled with, allow toggling constrained_layout as you suggest, and of course allow bailout to tight_layout instead.

WRT animations: definitely, constrained_layout will cause the axes to be resized while animating. Again, the current solution is to draw the first draw w/ constrained_layout on, and then call fig.do_constrained_layout(False) to turn it off for the animation steps.

@ImportanceOfBeingErnest
Copy link
Member

So a simple GUI toggle doesn't suffice, because you can't go back and remake the figure.

So that button would only appear if you created the figure with contrained_layout turned on.

Again, the current solution is to draw the first draw w/ constrained_layout on, and then call fig.do_constrained_layout(False) to turn it off for the animation steps.

Sounds like this needs a chapter in the constrained_layout guide.

@jklymak
Copy link
Member Author

jklymak commented Jul 13, 2018

OK, for now I think the right thing to do is whats in #11627, which is to turn CL off during ZOOM and PAN GUI events... Ummm, maybe I mixed things, but in #11648 I added a paragraph to the guide about turning CL on and off ie. for animation.

@jklymak jklymak closed this as completed Dec 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: geometry manager LayoutEngine, Constrained layout, Tight layout
Projects
None yet
Development

No branches or pull requests

3 participants