diff --git a/lib/matplotlib/_constrained_layout.py b/lib/matplotlib/_constrained_layout.py index 5623e12a3c41..f5f23581bd9d 100644 --- a/lib/matplotlib/_constrained_layout.py +++ b/lib/matplotlib/_constrained_layout.py @@ -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() @@ -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): """ diff --git a/lib/matplotlib/tests/test_constrainedlayout.py b/lib/matplotlib/tests/test_constrainedlayout.py index 7c7dd43a3115..df2dbd6f43bd 100644 --- a/lib/matplotlib/tests/test_constrainedlayout.py +++ b/lib/matplotlib/tests/test_constrainedlayout.py @@ -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)