Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix contrained layout applying pad multiple times #30108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions 41 lib/matplotlib/_constrained_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,14 +538,10 @@ def match_submerged_margins(layoutgrids, fig):

# interior columns:
if len(ss1.colspan) > 1:
maxsubl = np.max(
lg1.margin_vals['left'][ss1.colspan[1:]] +
lg1.margin_vals['leftcb'][ss1.colspan[1:]]
)
maxsubr = np.max(
lg1.margin_vals['right'][ss1.colspan[:-1]] +
lg1.margin_vals['rightcb'][ss1.colspan[:-1]]
)
leftcb = lg1.margin_vals['leftcb'][ss1.colspan[1:]]
rightcb = lg1.margin_vals['rightcb'][ss1.colspan[:-1]]
maxsubl = np.max(lg1.margin_vals['left'][ss1.colspan[1:]] + leftcb)
maxsubr = np.max(lg1.margin_vals['right'][ss1.colspan[:-1]] + rightcb)
for ax2 in axs:
ss2 = ax2.get_subplotspec()
lg2 = layoutgrids[ss2.get_gridspec()]
Expand All @@ -560,22 +556,17 @@ def match_submerged_margins(layoutgrids, fig):
lg2.margin_vals['rightcb'][ss2.colspan[:-1]])
if maxsubr2 > maxsubr:
maxsubr = maxsubr2
for i in ss1.colspan[1:]:
lg1.edit_margin_min('left', maxsubl, cell=i)
for i in ss1.colspan[:-1]:
lg1.edit_margin_min('right', maxsubr, cell=i)
for i, cb in zip(ss1.colspan[1:], leftcb):
lg1.edit_margin_min('left', maxsubl - cb, cell=i)
for i, cb in zip(ss1.colspan[:-1], rightcb):
lg1.edit_margin_min('right', maxsubr - cb, cell=i)

# interior rows:
if len(ss1.rowspan) > 1:
maxsubt = np.max(
lg1.margin_vals['top'][ss1.rowspan[1:]] +
lg1.margin_vals['topcb'][ss1.rowspan[1:]]
)
maxsubb = np.max(
lg1.margin_vals['bottom'][ss1.rowspan[:-1]] +
lg1.margin_vals['bottomcb'][ss1.rowspan[:-1]]
)

topcb = lg1.margin_vals['topcb'][ss1.rowspan[1:]]
bottomcb = lg1.margin_vals['bottomcb'][ss1.rowspan[:-1]]
maxsubt = np.max(lg1.margin_vals['top'][ss1.rowspan[1:]] + topcb)
maxsubb = np.max(lg1.margin_vals['bottom'][ss1.rowspan[:-1]] + bottomcb)
for ax2 in axs:
ss2 = ax2.get_subplotspec()
lg2 = layoutgrids[ss2.get_gridspec()]
Expand All @@ -589,10 +580,10 @@ def match_submerged_margins(layoutgrids, fig):
lg2.margin_vals['bottom'][ss2.rowspan[:-1]] +
lg2.margin_vals['bottomcb'][ss2.rowspan[:-1]]
), maxsubb])
for i in ss1.rowspan[1:]:
lg1.edit_margin_min('top', maxsubt, cell=i)
for i in ss1.rowspan[:-1]:
lg1.edit_margin_min('bottom', maxsubb, cell=i)
for i, cb in zip(ss1.rowspan[1:], topcb):
lg1.edit_margin_min('top', maxsubt - cb, cell=i)
for i, cb in zip(ss1.rowspan[:-1], bottomcb):
lg1.edit_margin_min('bottom', maxsubb - cb, cell=i)

return axs

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions 34 lib/matplotlib/tests/test_constrainedlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,37 @@ def test_submerged_subfig():
for ax in axs[1:]:
assert np.allclose(ax.get_position().bounds[-1],
axs[0].get_position().bounds[-1], atol=1e-6)


def test_submerged_height_gap():
"""Test that the gap between rows does not depend on the number of columns."""

mosaic1 = "AC;BC"
mosaic2 = "ACDE;BCDE"

fig1, ax_dir1 = plt.subplot_mosaic(mosaic1, layout='constrained')
fig2, ax_dir2 = plt.subplot_mosaic(mosaic2, layout='constrained')
for fig in fig1, fig2:
fig.get_layout_engine().set(h_pad=0.2)
fig.draw_without_rendering()

for label in 'A', 'B':
np.testing.assert_allclose(ax_dir1[label].get_position().bounds[-1],
ax_dir2[label].get_position().bounds[-1])


def test_submerged_width_gap():
"""Test that the gap between columns does not depend on the number of rows."""

mosaic1 = "AB;CC"
mosaic2 = "AB;CC;DD"

fig1, ax_dir1 = plt.subplot_mosaic(mosaic1, layout='constrained')
fig2, ax_dir2 = plt.subplot_mosaic(mosaic2, layout='constrained')
for fig in fig1, fig2:
fig.get_layout_engine().set(w_pad=0.2)
fig.draw_without_rendering()

for label in 'A', 'B':
np.testing.assert_allclose(ax_dir1[label].get_position().bounds[-2],
ax_dir2[label].get_position().bounds[-2])
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.