From 93391963c17693dc3e72ca9a31fafd4b08b2f5fe Mon Sep 17 00:00:00 2001 From: Eric Firing Date: Sun, 25 Feb 2018 11:03:49 -1000 Subject: [PATCH] Backport PR #10594: FIX: colorbar check for constrained layout --- lib/matplotlib/colorbar.py | 8 ++++++-- lib/matplotlib/tests/test_constrainedlayout.py | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 9a91cef34932..867f6f54772b 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1132,8 +1132,12 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15, parents = np.atleast_1d(parents).ravel() # check if using constrained_layout: - gs = parents[0].get_subplotspec().get_gridspec() - using_constrained_layout = (gs._layoutbox is not None) + try: + gs = parents[0].get_subplotspec().get_gridspec() + using_constrained_layout = (gs._layoutbox is not None) + except AttributeError: + using_constrained_layout = False + # defaults are not appropriate for constrained_layout: pad0 = loc_settings['pad'] if using_constrained_layout: diff --git a/lib/matplotlib/tests/test_constrainedlayout.py b/lib/matplotlib/tests/test_constrainedlayout.py index ab720618fc97..9c36fb2476ee 100644 --- a/lib/matplotlib/tests/test_constrainedlayout.py +++ b/lib/matplotlib/tests/test_constrainedlayout.py @@ -377,3 +377,14 @@ def test_constrained_layout19(): ax.set_title('') fig.canvas.draw() assert all(ax.get_position().extents == ax2.get_position().extents) + + +def test_constrained_layout20(): + 'Smoke test cl does not mess up added axes' + gx = np.linspace(-5, 5, 4) + img = np.hypot(gx, gx[:, None]) + + fig = plt.figure() + ax = fig.add_axes([0, 0, 1, 1]) + mesh = ax.pcolormesh(gx, gx, img) + fig.colorbar(mesh)