Skip to content

Adding Text Annotation when Co-plotting Shrinks Subplots when Tight Layout is Applied #18098

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
Aerovet opened this issue Jul 28, 2020 · 4 comments · Fixed by #18100
Closed

Comments

@Aerovet
Copy link

Aerovet commented Jul 28, 2020

Bug report

Bug summary

Vertical annotations shrink subplots when a figure handle's tight_layout is set to True. The expectation is that annotations would overlap the subplots.

Looking at the documentation...

https://matplotlib.org/3.2.2/tutorials/intermediate/tight_layout_guide.html#caveats

tight_layout() only considers ticklabels, axis labels, and titles. Thus, other artists may be clipped and also may overlap

With that in mind, tight_layout should not consider Text annotations, and shouldn't adjust the subplot size when added.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,np.pi*2,1000)
y = np.sin(x)
dy = np.cos(x)
dy2 = -np.sin(x)

for b in [False,True]:

    h, ax = plt.subplots(3,1,sharex = True)
    h.set_tight_layout(b)
    ax[0].plot(x,y)
    ax[1].plot(x,dy)
    ax[2].plot(x,dy2)

    ax[2].annotate(s = f'Annotation When Tight Layout is {b}', xy = (np.pi,-1), rotation = 'vertical')

Actual outcome

image

Expected outcome

In this case, tight_layout is set to FALSE just to see visually what it looks like with the text overlayed

image

Matplotlib version

  • Operating system: CentOS Linux 7 (Core)
  • Matplotlib version: 3.2.2
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.8.3
  • Jupyter version (if applicable):
  • Other libraries: numpy 1.18.5

conda

conda-forge

@jklymak
Copy link
Member

jklymak commented Jul 28, 2020

This is an error in the documentation, in that it wasn't updated when this was changed.

As you can imagine, there are just as many complaints when the annotations are clipped or ignored, so we a) made the default that they are not ignored, and b) gave you a toggle to allow them to be ignored. All artists have a set_in_layout method, which you can set to False here.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,np.pi*2,1000)
y = np.sin(x)
dy = np.cos(x)
dy2 = -np.sin(x)

for b in [False,True]:

    h, ax = plt.subplots(3,1,sharex = True)
    h.set_tight_layout(b)
    ax[0].plot(x,y)
    ax[1].plot(x,dy)
    ax[2].plot(x,dy2)

    an = ax[2].annotate(s = f'Annotation When Tight Layout is {b}', xy = (np.pi,-1), rotation = 'vertical')
    an.set_in_layout(False)
    plt.show()

TL

@Aerovet
Copy link
Author

Aerovet commented Jul 28, 2020

@jklymak Thank you for the help on this one, and also the quick turn around. Yeah, its easy to imagine the complaints. Glad to hear that the solution to this was as easy as setting set_in_layout. Thanks again!

@jklymak jklymak linked a pull request Jul 28, 2020 that will close this issue
6 tasks
@jklymak
Copy link
Member

jklymak commented Jul 28, 2020

@bawood2 feel free to review #18100 to make sure it would have been clear... Thanks for the report!

@jklymak jklymak added this to the v3.3-doc milestone Jul 28, 2020
@Aerovet
Copy link
Author

Aerovet commented Jul 28, 2020

@jklymak I reviewed the doc updates in #18100, and agree that it would have been clear, especially with the prompt to use set_in_layout

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.

2 participants