From 5efddd3db6dbef5284b66ef13ec617067f6697e0 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 22 Sep 2022 17:34:03 -0400 Subject: [PATCH 1/2] FIX: do not set constrained layout on false-y values closes #23986 --- lib/matplotlib/figure.py | 5 ++++- lib/matplotlib/tests/test_constrainedlayout.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 5bbb786984d2..447f508194a1 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -2426,9 +2426,12 @@ def __init__(self, if isinstance(tight_layout, dict): self.get_layout_engine().set(**tight_layout) elif constrained_layout is not None: - self.set_layout_engine(layout='constrained') if isinstance(constrained_layout, dict): + self.set_layout_engine(layout='constrained') self.get_layout_engine().set(**constrained_layout) + elif constrained_layout: + self.set_layout_engine(layout='constrained') + else: # everything is None, so use default: self.set_layout_engine(layout=layout) diff --git a/lib/matplotlib/tests/test_constrainedlayout.py b/lib/matplotlib/tests/test_constrainedlayout.py index 35eb850fcd70..853bb9a2f820 100644 --- a/lib/matplotlib/tests/test_constrainedlayout.py +++ b/lib/matplotlib/tests/test_constrainedlayout.py @@ -656,3 +656,14 @@ def test_compressed1(): pos = axs[1, 2].get_position() np.testing.assert_allclose(pos.x1, 0.8618, atol=1e-3) np.testing.assert_allclose(pos.y0, 0.1934, atol=1e-3) + + +@pytest.mark.parametrize('arg, state', [ + (True, True), + (False, False), + ({}, True), + ({'rect': None}, True) +]) +def test_set_constrained_lyout(arg, state): + fig, ax = plt.subplots(constrained_layout=arg) + assert fig.get_constrained_layout() is state From fe1aed2b85753b573c702ee71798f61a42a6efba Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 22 Sep 2022 20:40:15 -0400 Subject: [PATCH 2/2] Update lib/matplotlib/tests/test_constrainedlayout.py Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/tests/test_constrainedlayout.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_constrainedlayout.py b/lib/matplotlib/tests/test_constrainedlayout.py index 853bb9a2f820..64906b74c3ff 100644 --- a/lib/matplotlib/tests/test_constrainedlayout.py +++ b/lib/matplotlib/tests/test_constrainedlayout.py @@ -664,6 +664,6 @@ def test_compressed1(): ({}, True), ({'rect': None}, True) ]) -def test_set_constrained_lyout(arg, state): +def test_set_constrained_layout(arg, state): fig, ax = plt.subplots(constrained_layout=arg) assert fig.get_constrained_layout() is state