Skip to content

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

Closed
@s-bear

Description

@s-bear

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.
test-constrained-True

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')

test-constrained-False

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions