From c682ca40c647770a967b6b8a7615eb91c7cb3fc9 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 7 Jan 2022 15:12:44 +0100 Subject: [PATCH 1/3] FIX: better repr for subgridspecs --- lib/matplotlib/_constrained_layout.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/_constrained_layout.py b/lib/matplotlib/_constrained_layout.py index 8b34acdc4ca8..8d90b678cd71 100644 --- a/lib/matplotlib/_constrained_layout.py +++ b/lib/matplotlib/_constrained_layout.py @@ -203,15 +203,17 @@ def make_layoutgrids_gs(layoutgrids, gs): if parentgs not in layoutgrids: layoutgrids = make_layoutgrids_gs(layoutgrids, parentgs) subspeclb = layoutgrids[parentgs] + # get a unique representation: + rep = object.__repr__(gs) + 'top' # gridspecfromsubplotspec need an outer container: - if f'{gs}top' not in layoutgrids: - layoutgrids[f'{gs}top'] = mlayoutgrid.LayoutGrid( + 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(), From cc034ad668991531554b70db36beceeb9112e8f3 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 7 Jan 2022 15:17:13 +0100 Subject: [PATCH 2/3] FIX: add test for identical subgridspec --- .../tests/test_constrainedlayout.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) 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( From 1c5796a6c84e0adf17148cab5124f9041116709c Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Sun, 9 Jan 2022 10:12:51 +0100 Subject: [PATCH 3/3] FIX --- lib/matplotlib/_constrained_layout.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/_constrained_layout.py b/lib/matplotlib/_constrained_layout.py index 8d90b678cd71..70f595c989ff 100644 --- a/lib/matplotlib/_constrained_layout.py +++ b/lib/matplotlib/_constrained_layout.py @@ -203,9 +203,9 @@ def make_layoutgrids_gs(layoutgrids, gs): if parentgs not in layoutgrids: layoutgrids = make_layoutgrids_gs(layoutgrids, parentgs) subspeclb = layoutgrids[parentgs] - # get a unique representation: - rep = object.__repr__(gs) + 'top' # gridspecfromsubplotspec need an outer container: + # get a unique representation: + rep = (gs, 'top') if rep not in layoutgrids: layoutgrids[rep] = mlayoutgrid.LayoutGrid( parent=subspeclb,