Skip to content

[Bug]: calling fig.tight_layout multiple times  #26290

Closed

Description

@tacaswell

Bug summary

Calling fig.tight_layout() multiple times warns.

Code for reproduction

import matplotlib.pyplot as plt

fig = plt.figure()
fig.tight_layout()
fig.tight_layout()

Actual outcome

<ipython-input-9-0981ef8afcc1>:5: UserWarning: The figure layout has changed to tight
  fig.tight_layout()

Expected outcome

no-warning.

Additional information

does not show up in 3.7.1, does show up in 3.7.2. Have not bisected this yet (or checked main).

From looking at the code I suspect
6a82f38 / #25626 / #25624 which is from me 😞 .

xref #25624

I suspect the fix is to not warn if we set the place holder due to fig.tight_layout.

Operating system

Arch

Matplotlib Version

3.7.2

Matplotlib Backend

any

Python version

3.11

Jupyter version

No response

Installation

pip

Activity

  1. added this to the v3.7.3 milestone on Jul 12, 2023
  2. tacaswell commented on Jul 12, 2023

    @tacaswell
    MemberAuthor

    likely related to posit-dev/py-shiny#611

  3. added 2 commits that reference this issue on Jul 12, 2023
    18e19b3
    f7574a4
  4. lcrmorin commented on Aug 20, 2023

    @lcrmorin

    Any way to avoid the message in the code - without having to disable warnings - ? (defining the layout explicitly ?)

  5. jklymak commented on Aug 20, 2023

    @jklymak
    Member

    If you are just using Matplotlib, don't call tight layout multiple times, or better use plt.figure(..., layout='constrained'). However, if you are using a downstream library that calls tight layout for you then you are somewhat stuck

  6. added a commit that references this issue on Sep 12, 2023
  7. crchinn commented on Oct 20, 2023

    @crchinn

    There is a major problem with tight_layout. If you call it on a figure, then close that figure and open a new figure, frequently the following figures get messed up. Setting tight_layout to False does not fix this.

  8. jklymak commented on Oct 21, 2023

    @jklymak
    Member

    @crchinn please open a new issue if you have problems with tight_layout wth reproducible self contained examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: geometry managerLayoutEngine, Constrained layout, Tight layout

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions