Skip to content

[Bug]: Top of plot clipped when using Subfigures without suptitle #21839

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

Closed
sponde25 opened this issue Dec 2, 2021 · 3 comments · Fixed by #21845
Closed

[Bug]: Top of plot clipped when using Subfigures without suptitle #21839

sponde25 opened this issue Dec 2, 2021 · 3 comments · Fixed by #21845
Labels
status: confirmed bug topic: geometry manager LayoutEngine, Constrained layout, Tight layout
Milestone

Comments

@sponde25
Copy link

sponde25 commented Dec 2, 2021

Bug summary

When using subfigures without setting a suptitle for the root figure, the top of the plot is clipped out. This includes the suptitle for the top subfigure.

Code for reproduction

def example_plot(ax, fontsize=12, hide_labels=False):
    pc = ax.pcolormesh(np.random.randn(30, 30), vmin=-2.5, vmax=2.5)
    if not hide_labels:
        ax.set_xlabel('x-label', fontsize=fontsize)
        ax.set_ylabel('y-label', fontsize=fontsize)
        ax.set_title('Title', fontsize=fontsize)
    return pc

np.random.seed(19680808)
# gridspec inside gridspec
fig = plt.figure(constrained_layout=True, figsize=(10, 4))
subfigs = fig.subfigures(1, 2, wspace=0.07)

axsLeft = subfigs[0].subplots(1, 2, sharey=True)
subfigs[0].set_facecolor('0.75')
for ax in axsLeft:
    pc = example_plot(ax)
subfigs[0].suptitle('Left plots', fontsize='x-large')
subfigs[0].colorbar(pc, shrink=0.6, ax=axsLeft, location='bottom')

axsRight = subfigs[1].subplots(3, 1, sharex=True)
for nn, ax in enumerate(axsRight):
    pc = example_plot(ax, hide_labels=True)
    if nn == 2:
        ax.set_xlabel('xlabel')
    if nn == 1:
        ax.set_ylabel('ylabel')

subfigs[1].set_facecolor('0.85')
subfigs[1].colorbar(pc, shrink=0.6, ax=axsRight)
subfigs[1].suptitle('Right plots', fontsize='x-large')

# fig.suptitle('Figure suptitle', fontsize='xx-large')
plt.show()

Actual outcome

bug_report

Expected outcome

expected_report

Additional information

No response

Operating system

Ubuntu

Matplotlib Version

3.4.3

Matplotlib Backend

module://ipykernel.pylab.backend_inline

Python version

3.8

Jupyter version

6.0.3

Installation

conda

@jklymak jklymak added topic: geometry manager LayoutEngine, Constrained layout, Tight layout status: confirmed bug labels Dec 2, 2021
@jklymak jklymak added this to the v3.6.0 milestone Dec 2, 2021
@jklymak
Copy link
Member

jklymak commented Dec 2, 2021

OK, this bug is reproducible with fig.savefig('boo.png', bbox_inches='tight'). Apparently the subfigure subtitles are not making it into the bbox calculation.

@sponde25
Copy link
Author

sponde25 commented Dec 2, 2021

A viable workaround is to place fig.suptitle(' ') at the end. It adds unnecessary whitespace but at least everything is visible.

@jklymak
Copy link
Member

jklymak commented Dec 2, 2021

If you save the plot normally (i.e. w/o bbox_inches='tight') it works fine. The only reason you are seeing clipping is because the inline backend clips to visible artists. The todo here is to make sure subfigure suptitles end up in the bbox search tree.

@jklymak jklymak mentioned this issue Dec 2, 2021
6 tasks
@jklymak jklymak modified the milestones: v3.6.0, v3.5.1 Dec 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: confirmed bug topic: geometry manager LayoutEngine, Constrained layout, Tight layout
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants