Skip to content

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

Closed

Description

@ImportanceOfBeingErnest

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

Activity

  1. jklymak commented on Dec 3, 2019

    @jklymak
    Member

    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.

  2. added this to the v3.3.0 milestone on Dec 3, 2019
  3. jklymak commented on May 23, 2020

    @jklymak
    Member

    See #17347

  4. modified the milestones: v3.3.0, v3.4.0 on May 25, 2020
  5. jklymak commented on May 25, 2020

    @jklymak
    Member

    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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions