From 6a82f38fe06bd40bc7dc2426dc8953a94a06e70d Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson Date: Wed, 5 Apr 2023 17:00:49 +0200 Subject: [PATCH] Backport PR #25624: FIX: correctly unset the layout engine in Figure.tight_layout --- lib/matplotlib/figure.py | 2 +- lib/matplotlib/tests/test_figure.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index d419d5cf3a97..def2418d8ea8 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -3512,7 +3512,7 @@ def tight_layout(self, *, pad=1.08, h_pad=None, w_pad=None, rect=None): and previous_engine is not None: _api.warn_external('The figure layout has changed to tight') finally: - self.set_layout_engine(None) + self.set_layout_engine('none') def figaspect(arg): diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 8c31e79082ce..f0b46483d697 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -621,6 +621,15 @@ def test_invalid_layouts(): fig.set_layout_engine("constrained") +@check_figures_equal(extensions=["png"]) +def test_tightlayout_autolayout_deconflict(fig_test, fig_ref): + for fig, autolayout in zip([fig_ref, fig_test], [False, True]): + with mpl.rc_context({'figure.autolayout': autolayout}): + axes = fig.subplots(ncols=2) + fig.tight_layout(w_pad=10) + assert isinstance(fig.get_layout_engine(), PlaceHolderLayoutEngine) + + @pytest.mark.parametrize('layout', ['constrained', 'compressed']) def test_layout_change_warning(layout): """