diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 555730245d7c..7b273cf9fb81 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -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): @@ -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( diff --git a/lib/matplotlib/tests/test_constrainedlayout.py b/lib/matplotlib/tests/test_constrainedlayout.py index 64906b74c3ff..b0833052ad6e 100644 --- a/lib/matplotlib/tests/test_constrainedlayout.py +++ b/lib/matplotlib/tests/test_constrainedlayout.py @@ -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() diff --git a/lib/matplotlib/tests/test_tightlayout.py b/lib/matplotlib/tests/test_tightlayout.py index 1eb7b4b4534f..968f0da7b514 100644 --- a/lib/matplotlib/tests/test_tightlayout.py +++ b/lib/matplotlib/tests/test_tightlayout.py @@ -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()