[Python] Why can't subfigures be nested in gridspecs to keep their suptitles separate in...

Discussão em 'Python' iniciado por Stack, Janeiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I would expect this code:

    import matplotlib.pyplot as plt

    fig = plt.figure(figsize=(8, 6))

    fig_gridspec = fig.add_gridspec(1, 1)

    top_subfig = fig.add_subfigure(fig_gridspec[(0, 0)])

    top_subfig.suptitle("I am the top subfig")

    top_subfig_gridspec = top_subfig.add_gridspec(1, 1, top=.7)

    nested_subfig = top_subfig.add_subfigure(top_subfig_gridspec[(0, 0)])

    nested_subfig.suptitle("I am the nested subfig")

    plt.show()


    to generate two suptitles on different lines. Instead, they are overlapping.

    [​IMG]

    Can anyone explain why? Also, is there a way to achieve such separation with nested subfigures?

    Edit: To be clear, I mean, without changing the dimensions of the grid in the gridspec.

    I know I can do this, and it might be what I wind up doing:

    import matplotlib.pyplot as plt

    fig = plt.figure(figsize=(8, 6))

    fig_gridspec = fig.add_gridspec(1, 1)

    top_subfig = fig.add_subfigure(fig_gridspec[(0, 0)])

    top_subfig.suptitle("I am the top subfig")

    top_subfig_gridspec = top_subfig.add_gridspec(2, 1, height_ratios=[.1, 1])

    nested_subfig = top_subfig.add_subfigure(top_subfig_gridspec[(1, 0)])

    nested_subfig.suptitle("I am the nested subfig")

    plt.show()


    I just don't understand why my first code block doesn't work, and why there seems to be no way to adjust a nested subfigures's position within another subfigure.

    Continue reading...

Compartilhe esta Página