You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Layout Managers are confused by complex arrangement of sub-figures and gridspec's #30076
Hmmm, well somehow the margin for the first subfigure is the starting point for the second figure, and then the margin gets made even larger for the second.
import matplotlib.pyplot as plt
def test(layout, labelled=False):
ll = labelled
fig = plt.figure(figsize=(4, 5), layout=layout)
fig.suptitle(f'Labelled = {not labelled}', fontsize='small')
figures = fig.subfigures(2, 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()
I played with this for a while, and I do not see any obvious error in the logic.
At its root, I think this is a limitation with the we we have chosen to set up the constraint solver, in that the behaviour above is following the constraints we have given it, but they are not rigorous enough to narrow the gap.
I appreciate that it would be more sensible for the subfigures to not care about each other's layout, but practically, that is not how the constraint engine is set up, and the constraints are all solved at the same time.
If you want deterministic behaviour here without changing the internals of constrained layout, you can simply create more subfigures so that the left-hand axes are not tied to what the right-hand column is doing:
We can keep this open, but not sure anything will be done about it in the near future. If we were to do anything, it might be to try and make subfigure1 independent of subfigure2. The other thing that could be improved is the assignment of space between axes in one column when the other column does not have any axes spines at that row (eg the case here). That case is mildly under constrained, and as shown here doesn't work consistently.
Activity
tacaswell commentedon May 19, 2025
I have seen similar things with code that set a fixed aspect ratio and manually adjusted the locations before running the layout code.
dropping to either the figures rows or cols being 1 makes it work and
is broken even worse
jklymak commentedon May 19, 2025
Hmmm, well somehow the margin for the first subfigure is the starting point for the second figure, and then the margin gets made even larger for the second.
No label
big label
jklymak commentedon May 19, 2025
I played with this for a while, and I do not see any obvious error in the logic.
At its root, I think this is a limitation with the we we have chosen to set up the constraint solver, in that the behaviour above is following the constraints we have given it, but they are not rigorous enough to narrow the gap.
I appreciate that it would be more sensible for the subfigures to not care about each other's layout, but practically, that is not how the constraint engine is set up, and the constraints are all solved at the same time.
If you want deterministic behaviour here without changing the internals of constrained layout, you can simply create more subfigures so that the left-hand axes are not tied to what the right-hand column is doing:
We can keep this open, but not sure anything will be done about it in the near future. If we were to do anything, it might be to try and make subfigure1 independent of subfigure2. The other thing that could be improved is the assignment of space between axes in one column when the other column does not have any axes spines at that row (eg the case here). That case is mildly under constrained, and as shown here doesn't work consistently.
jklymak commentedon May 20, 2025
Actually I found the bug #30089 is an easy fix.