FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[Bug]: LayoutEngine rect not respected when saving figure with `rcParams['figure.constrained_layout.use'] = True` · Issue #24389 · matplotlib/matplotlib · GitHub

Repository navigation

[Bug]: LayoutEngine rect not respected when saving figure with rcParams['figure.constrained_layout.use'] = True #24389

Closed

Description

Bug summary

I use the rect parameter of the figure's layout engine to set figure margins. For figures with layout='constrained' this parameter appears to be ignored if rcParams['figure.constrained_layout.use'] is True when saving the figure.

Code for reproduction

import matplotlib.pyplot as plt
plt.rcParams['figure.constrained_layout.use']=True
fig, ax = plt.subplots()
engine = fig.get_layout_engine()
engine.set(rect=[0.1,0.1,0.8,0.8]) # (left, bottom, width, height)
engine.execute(fig)
fig.savefig('test-constrained-True.png')

Actual outcome

The saved figure does not have the expected layout. It does not match the figure displayed interactively. The axes extend to the edges of the figure area.

Expected outcome

The saved figure has matches what is shown interactively. The axes do not extend to the edges of the figure area, respecting the rect parameter. This saved figure is generated with the code, which does not demonstrate the bug:

plt.rcParams['figure.constrained_layout.use'] = False
fig, ax = plt.subplots(layout='constrained')
engine = fig.get_layout_engine()
engine.set(rect=[0.1,0.1,0.8,0.8]) # (left, bottom, width, height)
engine.execute(fig)
fig.savefig('test-constrained-False.png')

Additional information

No response

Operating system

Windows

Matplotlib Version

3.6.1

Matplotlib Backend

Qt5Agg

Python version

3.10.6

Jupyter version

n/a

Installation

conda

Activity

  1. jklymak commented on Nov 7, 2022

    Member

    What are you trying to accomplish by setting the rcParam? I'm surprised this has any effect on existing figures. If you want to turn constrained layout on or off I'd just do so manually.

  2. s-bear commented on Nov 7, 2022

    Author
  3. s-bear commented on Nov 7, 2022

    Author

    I just edited the bug report to make the example code more consise and demonstrate the issue more clearly. Cheers, Sam

  4. jklymak commented on Nov 7, 2022

    Member

    This was incorrect changing of the layout engine during the print stage, and was fixed in #24167 which was released with 3.6.2. Sorry this broke. Essentially a duplicate of #24133.

    BTW, you should not need the engine.execute(fig) step as that gets called upon either show or savefig

  5. tacaswell commented on Nov 7, 2022

    Member

    I can confirm that this is fixed in 3.6.2.

    Thank you for the very clear example @s-bear !

  6. added this to the v3.6.2 milestone on Nov 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL