Skip to content

Commit dca642d

Browse files
jklymakmeeseeksmachine
authored andcommitted
Backport PR #24924: Fix toggling layout engines
1 parent 95086b1 commit dca642d

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

lib/matplotlib/figure.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -2760,9 +2760,9 @@ def set_tight_layout(self, tight):
27602760
"""
27612761
if tight is None:
27622762
tight = mpl.rcParams['figure.autolayout']
2763+
_tight = 'tight' if bool(tight) else 'none'
27632764
_tight_parameters = tight if isinstance(tight, dict) else {}
2764-
if bool(tight):
2765-
self.set_layout_engine(TightLayoutEngine(**_tight_parameters))
2765+
self.set_layout_engine(_tight, **_tight_parameters)
27662766
self.stale = True
27672767

27682768
def get_constrained_layout(self):
@@ -2797,10 +2797,9 @@ def set_constrained_layout(self, constrained):
27972797
"""
27982798
if constrained is None:
27992799
constrained = mpl.rcParams['figure.constrained_layout.use']
2800-
_constrained = bool(constrained)
2800+
_constrained = 'constrained' if bool(constrained) else 'none'
28012801
_parameters = constrained if isinstance(constrained, dict) else {}
2802-
if _constrained:
2803-
self.set_layout_engine(ConstrainedLayoutEngine(**_parameters))
2802+
self.set_layout_engine(_constrained, **_parameters)
28042803
self.stale = True
28052804

28062805
@_api.deprecated(

lib/matplotlib/tests/test_constrainedlayout.py

+11
Original file line numberDiff line numberDiff line change
@@ -667,3 +667,14 @@ def test_compressed1():
667667
def test_set_constrained_layout(arg, state):
668668
fig, ax = plt.subplots(constrained_layout=arg)
669669
assert fig.get_constrained_layout() is state
670+
671+
672+
def test_constrained_toggle():
673+
fig, ax = plt.subplots()
674+
with pytest.warns(PendingDeprecationWarning):
675+
fig.set_constrained_layout(True)
676+
assert fig.get_constrained_layout()
677+
fig.set_constrained_layout(False)
678+
assert not fig.get_constrained_layout()
679+
fig.set_constrained_layout(True)
680+
assert fig.get_constrained_layout()

lib/matplotlib/tests/test_tightlayout.py

+11
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,14 @@ def test_tight_pads():
380380
def test_tight_kwargs():
381381
fig, ax = plt.subplots(tight_layout={'pad': 0.15})
382382
fig.draw_without_rendering()
383+
384+
385+
def test_tight_toggle():
386+
fig, ax = plt.subplots()
387+
with pytest.warns(PendingDeprecationWarning):
388+
fig.set_tight_layout(True)
389+
assert fig.get_tight_layout()
390+
fig.set_tight_layout(False)
391+
assert not fig.get_tight_layout()
392+
fig.set_tight_layout(True)
393+
assert fig.get_tight_layout()

0 commit comments

Comments
 (0)