Skip to content

FIX: fix submerged margins algorithm being applied twice #30089

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
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
8 changes: 6 additions & 2 deletions lib/matplotlib/_constrained_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,11 +521,13 @@ def match_submerged_margins(layoutgrids, fig):
See test_constrained_layout::test_constrained_layout12 for an example.
"""

axsdone = []
for sfig in fig.subfigs:
match_submerged_margins(layoutgrids, sfig)
axsdone += match_submerged_margins(layoutgrids, sfig)

axs = [a for a in fig.get_axes()
if a.get_subplotspec() is not None and a.get_in_layout()]
if (a.get_subplotspec() is not None and a.get_in_layout() and
a not in axsdone)]

for ax1 in axs:
ss1 = ax1.get_subplotspec()
Expand Down Expand Up @@ -592,6 +594,8 @@ def match_submerged_margins(layoutgrids, fig):
for i in ss1.rowspan[:-1]:
lg1.edit_margin_min('bottom', maxsubb, cell=i)

return axs


def get_cb_parent_spans(cbax):
"""
Expand Down
20 changes: 20 additions & 0 deletions lib/matplotlib/tests/test_constrainedlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,3 +721,23 @@ def test_layout_leak():
gc.collect()
assert not any(isinstance(obj, mpl._layoutgrid.LayoutGrid)
for obj in gc.get_objects())


def test_submerged_subfig():
"""
Test that the submerged margin logic does not get called multiple times
on same axes if it is already in a subfigure
"""
fig = plt.figure(figsize=(4, 5), layout='constrained')
figures = fig.subfigures(3, 1)
axs = []
for f in figures.flatten():
gs = f.add_gridspec(2, 2)
for i in range(2):
axs += [f.add_subplot(gs[i, 0])]
axs[-1].plot()
f.add_subplot(gs[:, 1]).plot()
fig.draw_without_rendering()
for ax in axs[1:]:
assert np.allclose(ax.get_position().bounds[-1],
axs[0].get_position().bounds[-1], atol=1e-6)
Loading