Skip to content

Conversation

jklymak
Copy link
Member

@jklymak jklymak commented May 20, 2025

Closes #30076

If a figure had subfigures, the submerged margin algorithm (that tries to deal with margins that are not shared across all axes) would get applied to all axes in the figure, including any that had already been taken care of in a subfigure. This lead to the erroneous behaviour described in #30076.

New version does not apply the algorithm if the axes has already been dealt with in a subfigure.

import matplotlib.pyplot as plt

def test(layout, labelled=False):
    ll = labelled
    fig = plt.figure(figsize=(4, 9), layout=layout)
    fig.suptitle(f'Labelled = {not labelled}', fontsize='small')
    figures = fig.subfigures(4, 1)
    for f in figures.flatten():
        gs = f.add_gridspec(2, 2)
        for i in range(2):
            ax = f.add_subplot(gs[i, 0])
            ax.plot()
            if not labelled:
                ax.set_xlabel('BOOO', fontsize='large')
                labelled = True
        f.add_subplot(gs[:, 1]).plot()
    fig.savefig(f'fig_{not ll}.png')
test("constrained")
#test("constrained", labelled=True)
plt.show()

This PR

now yields:
fig_True

On main

fig_True

Test just makes sure that the appropriate subplots are the same height now. The test fails on main.

@jklymak jklymak added this to the v3.10.4 milestone May 20, 2025
@rcomer rcomer merged commit 74ae218 into matplotlib:main May 25, 2025
39 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request May 25, 2025
QuLogic added a commit that referenced this pull request May 27, 2025
…089-on-v3.10.x

Backport PR #30089 on branch v3.10.x (FIX: fix submerged margins algorithm being applied twice)
@ksunden ksunden mentioned this pull request Aug 7, 2025
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: geometry manager LayoutEngine, Constrained layout, Tight layout
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Layout Managers are confused by complex arrangement of sub-figures and gridspec's
3 participants