diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 876b1d114e1b..9aed565bebc8 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -3522,14 +3522,14 @@ def tight_layout(self, *, pad=1.08, h_pad=None, w_pad=None, rect=None): # note that here we do not permanently set the figures engine to # tight_layout but rather just perform the layout in place and remove # any previous engines. - engine = TightLayoutEngine(pad=pad, h_pad=h_pad, w_pad=w_pad, - rect=rect) + engine = TightLayoutEngine(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect) try: previous_engine = self.get_layout_engine() self.set_layout_engine(engine) engine.execute(self) - if not isinstance(previous_engine, TightLayoutEngine) \ - and previous_engine is not None: + if previous_engine is not None and not isinstance( + previous_engine, (TightLayoutEngine, PlaceHolderLayoutEngine) + ): _api.warn_external('The figure layout has changed to tight') finally: self.set_layout_engine('none') diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 63117ce121a5..cfd5f27de13a 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -701,6 +701,14 @@ def test_layout_change_warning(layout): plt.tight_layout() +def test_repeated_tightlayout(): + fig = Figure() + fig.tight_layout() + # subsequent calls should not warn + fig.tight_layout() + fig.tight_layout() + + @check_figures_equal(extensions=["png", "pdf"]) def test_add_artist(fig_test, fig_ref): fig_test.dpi = 100