Skip to content

Backport PR #24924 on branch v3.7.x (Fix toggling layout engines) #24933

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2760,9 +2760,9 @@ def set_tight_layout(self, tight):
"""
if tight is None:
tight = mpl.rcParams['figure.autolayout']
_tight = 'tight' if bool(tight) else 'none'
_tight_parameters = tight if isinstance(tight, dict) else {}
if bool(tight):
self.set_layout_engine(TightLayoutEngine(**_tight_parameters))
self.set_layout_engine(_tight, **_tight_parameters)
self.stale = True

def get_constrained_layout(self):
Expand Down Expand Up @@ -2797,10 +2797,9 @@ def set_constrained_layout(self, constrained):
"""
if constrained is None:
constrained = mpl.rcParams['figure.constrained_layout.use']
_constrained = bool(constrained)
_constrained = 'constrained' if bool(constrained) else 'none'
_parameters = constrained if isinstance(constrained, dict) else {}
if _constrained:
self.set_layout_engine(ConstrainedLayoutEngine(**_parameters))
self.set_layout_engine(_constrained, **_parameters)
self.stale = True

@_api.deprecated(
Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_constrainedlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,14 @@ def test_compressed1():
def test_set_constrained_layout(arg, state):
fig, ax = plt.subplots(constrained_layout=arg)
assert fig.get_constrained_layout() is state


def test_constrained_toggle():
fig, ax = plt.subplots()
with pytest.warns(PendingDeprecationWarning):
fig.set_constrained_layout(True)
assert fig.get_constrained_layout()
fig.set_constrained_layout(False)
assert not fig.get_constrained_layout()
fig.set_constrained_layout(True)
assert fig.get_constrained_layout()
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_tightlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,14 @@ def test_tight_pads():
def test_tight_kwargs():
fig, ax = plt.subplots(tight_layout={'pad': 0.15})
fig.draw_without_rendering()


def test_tight_toggle():
fig, ax = plt.subplots()
with pytest.warns(PendingDeprecationWarning):
fig.set_tight_layout(True)
assert fig.get_tight_layout()
fig.set_tight_layout(False)
assert not fig.get_tight_layout()
fig.set_tight_layout(True)
assert fig.get_tight_layout()