From f215d09c029da124a90f735aeeddf4131b203575 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Mon, 29 Jul 2019 20:20:14 -0700 Subject: [PATCH 1/2] FIX: let CL work with hidden axes --- lib/matplotlib/_constrained_layout.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/_constrained_layout.py b/lib/matplotlib/_constrained_layout.py index b9fac97d30a8..ab156217b859 100644 --- a/lib/matplotlib/_constrained_layout.py +++ b/lib/matplotlib/_constrained_layout.py @@ -272,7 +272,11 @@ def _make_layout_margins(ax, renderer, h_pad, w_pad): invTransFig = fig.transFigure.inverted().transform_bbox pos = ax.get_position(original=True) tightbbox = ax.get_tightbbox(renderer=renderer) - bbox = invTransFig(tightbbox) + if tightbbox is None: + bbox = pos + else: + bbox = invTransFig(tightbbox) + # this can go wrong: if not (np.isfinite(bbox.width) and np.isfinite(bbox.height)): # just abort, this is likely a bad set of co-ordinates that From c8ec3c67fe51f2c7a2d7d4e384597af06a672c0d Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Tue, 6 Aug 2019 08:23:44 -0700 Subject: [PATCH 2/2] TST: test hidden axes Update lib/matplotlib/tests/test_constrainedlayout.py Co-Authored-By: David Stansby FLAKE8 --- lib/matplotlib/tests/test_constrainedlayout.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/matplotlib/tests/test_constrainedlayout.py b/lib/matplotlib/tests/test_constrainedlayout.py index 1585cd9dbf22..c91037eaed27 100644 --- a/lib/matplotlib/tests/test_constrainedlayout.py +++ b/lib/matplotlib/tests/test_constrainedlayout.py @@ -387,3 +387,16 @@ def test_colorbar_location(): fig.colorbar(pcm, ax=axs[-2, 3:], shrink=0.5, location='top') fig.colorbar(pcm, ax=axs[0, 0], shrink=0.5, location='left') fig.colorbar(pcm, ax=axs[1:3, 2], shrink=0.5, location='right') + + +def test_hidden_axes(): + # test that if we make an axes not visible that constrained_layout + # still works. Note the axes still takes space in the layout + # (as does a gridspec slot that is empty) + fig, axs = plt.subplots(2, 2, constrained_layout=True) + axs[0, 1].set_visible(False) + fig.canvas.draw() + extents1 = np.copy(axs[0, 0].get_position().extents) + + np.testing.assert_allclose(extents1, + [0.045552, 0.548288, 0.47319, 0.982638], rtol=1e-5)