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

Skip to content

Commit 5ee9553

Browse files
authored
Merge pull request #10371 from jklymak/fix-constrainedlayout-uneven
FIX: constrainedlayout uneven grid specs
2 parents 605fd3c + 698cf7f commit 5ee9553

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -361,15 +361,19 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
361361
'bottom')
362362

363363
###########
364-
# Now we make the widths and heights similar.
364+
# Now we make the widths and heights of position boxes
365+
# similar. (i.e the spine locations)
365366
# This allows vertically stacked subplots to have
366-
# different sizes if they occupy different ammounts
367+
# different sizes if they occupy different amounts
367368
# of the gridspec: i.e.
368369
# gs = gridspec.GridSpec(3,1)
369370
# ax1 = gs[0,:]
370371
# ax2 = gs[1:,:]
371372
# then drows0 = 1, and drowsC = 2, and ax2
372373
# should be at least twice as large as ax1.
374+
# But it can be more than twice as large because
375+
# it needs less room for the labeling.
376+
#
373377
# For height, this only needs to be done if the
374378
# subplots share a column. For width if they
375379
# share a row.
@@ -387,31 +391,41 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
387391
dcolsC = (colnumCmax - colnumCmin + 1)
388392
dcols0 = (colnum0max - colnum0min + 1)
389393

390-
if drowsC > drows0:
394+
if height0 > heightC:
391395
if in_same_column(ss0, ssc):
392396
ax._poslayoutbox.constrain_height_min(
393-
axc._poslayoutbox.height * drows0 * height0
394-
/ drowsC / heightC)
395-
elif drowsC < drows0:
396-
if in_same_column(ss0, ssc):
397+
axc._poslayoutbox.height * height0 / heightC)
398+
# these constraints stop the smaller axes from
399+
# being allowed to go to zero height...
397400
axc._poslayoutbox.constrain_height_min(
398-
ax._poslayoutbox.height * drowsC * heightC
399-
/ drows0 / drowsC)
401+
ax._poslayoutbox.height * heightC /
402+
(height0*1.8))
400403
else:
404+
if in_same_column(ss0, ssc):
405+
axc._poslayoutbox.constrain_height_min(
406+
ax._poslayoutbox.height * heightC / height0)
407+
ax._poslayoutbox.constrain_height_min(
408+
ax._poslayoutbox.height * height0 /
409+
(heightC*1.8))
410+
if drows0 == drowsC:
401411
ax._poslayoutbox.constrain_height(
402412
axc._poslayoutbox.height * height0 / heightC)
403413
# widths...
404-
if dcolsC > dcols0:
414+
if width0 > widthC:
405415
if in_same_row(ss0, ssc):
406416
ax._poslayoutbox.constrain_width_min(
407-
axc._poslayoutbox.width * dcols0 * width0
408-
/ dcolsC / widthC)
409-
elif dcolsC < dcols0:
410-
if in_same_row(ss0, ssc):
417+
axc._poslayoutbox.width * width0 / widthC)
411418
axc._poslayoutbox.constrain_width_min(
412-
ax._poslayoutbox.width * dcolsC * widthC
413-
/ dcols0 / width0)
419+
ax._poslayoutbox.width * widthC /
420+
(width0*1.8))
414421
else:
422+
if in_same_row(ss0, ssc):
423+
axc._poslayoutbox.constrain_width_min(
424+
ax._poslayoutbox.width * widthC / width0)
425+
ax._poslayoutbox.constrain_width_min(
426+
axc._poslayoutbox.width * width0 /
427+
(widthC*1.8))
428+
if dcols0 == dcolsC:
415429
ax._poslayoutbox.constrain_width(
416430
axc._poslayoutbox.width * width0 / widthC)
417431

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,21 @@ def test_constrained_layout16():
345345
fig, ax = plt.subplots(constrained_layout=True)
346346
example_plot(ax, fontsize=12)
347347
ax2 = fig.add_axes([0.2, 0.2, 0.4, 0.4])
348+
349+
350+
@image_comparison(baseline_images=['constrained_layout17'],
351+
extensions=['png'])
352+
def test_constrained_layout17():
353+
'Test uneven gridspecs'
354+
fig = plt.figure(constrained_layout=True)
355+
gs = gridspec.GridSpec(3, 3, figure=fig)
356+
357+
ax1 = fig.add_subplot(gs[0, 0])
358+
ax2 = fig.add_subplot(gs[0, 1:])
359+
ax3 = fig.add_subplot(gs[1:, 0:2])
360+
ax4 = fig.add_subplot(gs[1:, -1])
361+
362+
example_plot(ax1)
363+
example_plot(ax2)
364+
example_plot(ax3)
365+
example_plot(ax4)

0 commit comments

Comments
 (0)