Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 75a8bb6

Browse files
timhoffmmeeseeksmachine
authored andcommitted
Backport PR #22144: Fix cl subgridspec
1 parent 61af908 commit 75a8bb6

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,16 @@ def make_layoutgrids_gs(layoutgrids, gs):
204204
layoutgrids = make_layoutgrids_gs(layoutgrids, parentgs)
205205
subspeclb = layoutgrids[parentgs]
206206
# gridspecfromsubplotspec need an outer container:
207-
if f'{gs}top' not in layoutgrids:
208-
layoutgrids[f'{gs}top'] = mlayoutgrid.LayoutGrid(
207+
# get a unique representation:
208+
rep = (gs, 'top')
209+
if rep not in layoutgrids:
210+
layoutgrids[rep] = mlayoutgrid.LayoutGrid(
209211
parent=subspeclb,
210212
name='top',
211213
nrows=1, ncols=1,
212214
parent_pos=(subplot_spec.rowspan, subplot_spec.colspan))
213215
layoutgrids[gs] = mlayoutgrid.LayoutGrid(
214-
parent=layoutgrids[f'{gs}top'],
216+
parent=layoutgrids[rep],
215217
name='gridspec',
216218
nrows=gs._nrows, ncols=gs._ncols,
217219
width_ratios=gs.get_width_ratios(),

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,26 @@ def test_constrained_layout6():
115115
ticks=ticker.MaxNLocator(nbins=5))
116116

117117

118+
def test_identical_subgridspec():
119+
120+
fig = plt.figure(constrained_layout=True)
121+
122+
GS = fig.add_gridspec(2, 1)
123+
124+
GSA = GS[0].subgridspec(1, 3)
125+
GSB = GS[1].subgridspec(1, 3)
126+
127+
axa = []
128+
axb = []
129+
for i in range(3):
130+
axa += [fig.add_subplot(GSA[i])]
131+
axb += [fig.add_subplot(GSB[i])]
132+
133+
fig.draw_without_rendering()
134+
# chech first row above second
135+
assert axa[0].get_position().y0 > axb[0].get_position().y1
136+
137+
118138
def test_constrained_layout7():
119139
"""Test for proper warning if fig not set in GridSpec"""
120140
with pytest.warns(

0 commit comments

Comments
 (0)