From 75a8bb601c681ff5fc24f3d1728b85deb3f3d383 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sun, 9 Jan 2022 13:19:47 +0100 Subject: [PATCH] Backport PR #22144: Fix cl subgridspec --- lib/matplotlib/_constrained_layout.py | 8 +++++--- .../tests/test_constrainedlayout.py | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/_constrained_layout.py b/lib/matplotlib/_constrained_layout.py index d360eb66164e..c998681b1db3 100644 --- a/lib/matplotlib/_constrained_layout.py +++ b/lib/matplotlib/_constrained_layout.py @@ -204,14 +204,16 @@ def make_layoutgrids_gs(layoutgrids, gs): layoutgrids = make_layoutgrids_gs(layoutgrids, parentgs) subspeclb = layoutgrids[parentgs] # gridspecfromsubplotspec need an outer container: - if f'{gs}top' not in layoutgrids: - layoutgrids[f'{gs}top'] = mlayoutgrid.LayoutGrid( + # get a unique representation: + rep = (gs, 'top') + if rep not in layoutgrids: + layoutgrids[rep] = mlayoutgrid.LayoutGrid( parent=subspeclb, name='top', nrows=1, ncols=1, parent_pos=(subplot_spec.rowspan, subplot_spec.colspan)) layoutgrids[gs] = mlayoutgrid.LayoutGrid( - parent=layoutgrids[f'{gs}top'], + parent=layoutgrids[rep], name='gridspec', nrows=gs._nrows, ncols=gs._ncols, width_ratios=gs.get_width_ratios(), diff --git a/lib/matplotlib/tests/test_constrainedlayout.py b/lib/matplotlib/tests/test_constrainedlayout.py index a717eace8348..8fd3cc5a35a7 100644 --- a/lib/matplotlib/tests/test_constrainedlayout.py +++ b/lib/matplotlib/tests/test_constrainedlayout.py @@ -115,6 +115,26 @@ def test_constrained_layout6(): ticks=ticker.MaxNLocator(nbins=5)) +def test_identical_subgridspec(): + + fig = plt.figure(constrained_layout=True) + + GS = fig.add_gridspec(2, 1) + + GSA = GS[0].subgridspec(1, 3) + GSB = GS[1].subgridspec(1, 3) + + axa = [] + axb = [] + for i in range(3): + axa += [fig.add_subplot(GSA[i])] + axb += [fig.add_subplot(GSB[i])] + + fig.draw_without_rendering() + # chech first row above second + assert axa[0].get_position().y0 > axb[0].get_position().y1 + + def test_constrained_layout7(): """Test for proper warning if fig not set in GridSpec""" with pytest.warns(