Skip to content

Should constrained_layout work as plt.figure() argument? #15821

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
ImportanceOfBeingErnest opened this issue Dec 3, 2019 · 3 comments · Fixed by #17347
Closed

Should constrained_layout work as plt.figure() argument? #15821

ImportanceOfBeingErnest opened this issue Dec 3, 2019 · 3 comments · Fixed by #17347
Assignees
Milestone

Comments

@ImportanceOfBeingErnest
Copy link
Member

Bug report

Bug summary

Might be a documentation issue. The constrainedlayout_guide says

constrained_layout needs to be activated before any axes are added to a figure. Two ways of doing so are

  • using the respective argument to subplots() or figure(),
  • [...]

However, fig = plt.figure(constrained_layout=True) does not seem to have any effect.

Code for reproduction

import matplotlib.pyplot as plt

def example_plot(ax, fontsize=12):
    ax.plot([1, 2])
    ax.set_xlabel('x-label', fontsize=fontsize)
    ax.set_ylabel('y-label', fontsize=fontsize)
    ax.set_title('Title', fontsize=fontsize)

fig = plt.figure(constrained_layout=True)
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)

for ax in [ax1, ax2]:
    example_plot(ax, fontsize=24)
plt.show()

Actual outcome

image

Expected outcome

image

Matplotlib version

  • Operating system: Windows 8.1
  • Matplotlib version: 3.1.0
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.7
@jklymak
Copy link
Member

jklymak commented Dec 3, 2019

Constrained layout doesn't work with add_subplot in the numeric form, only the gridspec form. i.e.

    fig = plt.figure(constrained_layout=True)
    gs = fig.add_gridspec(2, 1)
    ax1 = fig.add_subplot(gs[0])
    ax2 = fig.add_subplot(gs[1])

works fine.

Probably the docs should be clearer about that. TBH I didn't realize add_subplot worked with the numeric arguments.

@jklymak jklymak added this to the v3.3.0 milestone Dec 3, 2019
@QuLogic QuLogic assigned QuLogic and jklymak and unassigned QuLogic May 23, 2020
@jklymak
Copy link
Member

jklymak commented May 23, 2020

See #17347

@jklymak jklymak modified the milestones: v3.3.0, v3.4.0 May 25, 2020
@jklymak
Copy link
Member

jklymak commented May 25, 2020

Moved to 3.4 because I don't think this is time critical. OTOH if someone wanted to merge #17347 in time for 3.3 this would be a helpful addition

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.

3 participants