From 94397f4949511881dfc35fbab76cb237f6911424 Mon Sep 17 00:00:00 2001 From: j1642 <60148902+j1642@users.noreply.github.com> Date: Sun, 20 Nov 2022 22:54:03 -0500 Subject: [PATCH] BUG: Warn when an existing layout manager changes to tight layout --- lib/matplotlib/figure.py | 4 ++++ lib/matplotlib/tests/test_figure.py | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 147500dd5451..5a3a5df8d18b 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -3430,8 +3430,12 @@ def tight_layout(self, *, pad=1.08, h_pad=None, w_pad=None, rect=None): 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: + _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 e38a772fe81e..9c78f151c99c 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -598,6 +598,17 @@ def test_invalid_layouts(): fig.set_layout_engine("constrained") +@pytest.mark.parametrize('layout', ['constrained', 'compressed']) +def test_layout_change_warning(layout): + """ + Raise a warning when a previously assigned layout changes to tight using + plt.tight_layout(). + """ + fig, ax = plt.subplots(layout=layout) + with pytest.warns(UserWarning, match='The figure layout has changed to'): + plt.tight_layout() + + @check_figures_equal(extensions=["png", "pdf"]) def test_add_artist(fig_test, fig_ref): fig_test.dpi = 100
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: