Skip to content

Clarify that constrained_layout and tight_layout conflict with each other #17339

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
ericpre opened this issue May 6, 2020 · 8 comments · Fixed by #29584
Closed

Clarify that constrained_layout and tight_layout conflict with each other #17339

ericpre opened this issue May 6, 2020 · 8 comments · Fixed by #29584
Milestone

Comments

@ericpre
Copy link
Member

ericpre commented May 6, 2020

Bug report

Bug summary

Not sure if this an expected behaviour, but the colorbar seems to be ignored when using tight_layout. Example taken from the "Constrained Layout Guide" (https://matplotlib.org/3.1.3/tutorials/intermediate/constrainedlayout_guide.html#colorbars).

Code for reproduction

arr = np.arange(100).reshape((10, 10))
fig, axs = plt.subplots(2, 2, figsize=(4, 4), constrained_layout=True)
for ax in axs.flat:
    im = ax.pcolormesh(arr)
fig.colorbar(im, ax=axs, shrink=0.6)
plt.tight_layout()

Actual outcome

image

Expected outcome
This is the outcome before calling plt.tight_layout()

image

Matplotlib version

  • Operating system: Fedora 31
  • Matplotlib version: 3.1.3 from conda defaults (3.2.1 from conda-forge behave similarly)
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.7.6
  • Jupyter version (if applicable):
  • Other libraries:
@jklymak
Copy link
Member

jklymak commented May 6, 2020

Yes, thats expected, or at least understood. You can use constrained_layout instead...

Edit: tight_layout and constrained_layout are not compatible.

@QuLogic
Copy link
Member

QuLogic commented May 6, 2020

Maybe we should raise if calling tight_layout when the figure has constrained_layout enabled?

@jklymak
Copy link
Member

jklymak commented May 6, 2020

Thats not particularly the problem here; constrained_layout is a bit of a red herring. tight_layout just doesn't know about the big colorbar so ignores it. In the code above constrained_layout=True is ignored because tight_layout gets precedence (simply because its older).

@QuLogic
Copy link
Member

QuLogic commented May 7, 2020

In the code above constrained_layout=True is ignored because tight_layout gets precedence (simply because its older).

Does that generate a warning then?

@ericpre
Copy link
Member Author

ericpre commented May 7, 2020

If tight_layout and constrained_layout are not compatible, it would useful to have it documented - I can do this at some point.

Regardless of using constrained_layout or not, can you confirm the following: if I understand correctly, the colorbar is ignored by tight_layout because it is an instances of matplotlib.axes._axes.Axes and not matplotlib.axes._subplots.AxesSubplot?

@jklymak
Copy link
Member

jklymak commented May 7, 2020

Its says constrained_layout is to be used instead of tight_layout in quite a few places. They are not incompatible per se, just tight_layout cancels out constrained_layout.

We could raise a warning I suppose.

@tacaswell tacaswell changed the title Colorbar ignored when calling tight_layout Clarify that constrained_layout and tight_layout conflict with each other May 7, 2020
@tacaswell
Copy link
Member

At least on master we do warn:

/tmp/pyIDWTj1:8: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
/tmp/pyIDWTj1:8: UserWarning: This figure was using constrained_layout==True, but that is incompatible with subplots_adjust and or tight_layout: setting constrained_layout==False. 

This limitation of tight_layout was one of the motivations for writing constrained layout. I think action items here are:

  1. add a sentance to the top indicating that constrained layout and tight layout are alternatives
  2. add a sub-section to https://matplotlib.org/tutorials/intermediate/constrainedlayout_guide.html#limitations showing tight_layout breaking this example.

@tacaswell tacaswell added this to the v3.4.0 milestone May 7, 2020
@ericpre
Copy link
Member Author

ericpre commented May 7, 2020

Thanks for the explanations!
In the documentation, it is mentioned that both are alternatives but it is not clear, which one would be more useful. Even if there are many examples, it is not straightforward to work out which one would be more appropriate/robust. Maybe also add some simple guidelines or rule of thumb to the documentation?

@QuLogic QuLogic modified the milestones: v3.4.0, v3.5.0 Jan 27, 2021
@QuLogic QuLogic modified the milestones: v3.5.0, v3.6.0 Sep 25, 2021
@QuLogic QuLogic modified the milestones: v3.6.0, v3.6.1 Sep 14, 2022
@QuLogic QuLogic modified the milestones: v3.6.1, v3.6.2 Oct 6, 2022
@QuLogic QuLogic modified the milestones: v3.6.2, v3.6.3 Oct 27, 2022
@QuLogic QuLogic modified the milestones: v3.6.3, v3.7.0 Jan 11, 2023
@ksunden ksunden modified the milestones: v3.7.0, v3.7.1 Feb 14, 2023
@QuLogic QuLogic modified the milestones: v3.7.1, v3.7-doc Mar 4, 2023
@QuLogic QuLogic modified the milestones: v3.7-doc, v3.8-doc Sep 9, 2023
timhoffm added a commit to timhoffm/matplotlib that referenced this issue Feb 6, 2025
@rcomer rcomer closed this as completed in 293eff8 Feb 28, 2025
@QuLogic QuLogic modified the milestones: v3.8-doc, v3.10.2 Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants