[Python] Subfigure ratios unexpectedly changed when using subplot_mosaic

Discussão em 'Python' iniciado por Stack, Fevereiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to do a set of plots using subplot_mosaic. The idea is that each row will consists of several large plots, as well as some 'sub-rows' of smaller plots, a minimal example is as follows:


    import matplotlib.pyplot as plt

    def test(pdf :pdfPages):

    fig = plt.figure(
    layout="constrained",
    figsize = (20, 20)
    )

    row = 3
    subcol = 3
    subrow = 3
    col = 1
    subfigs = fig.subfigures(nrows=row, ncols=col)
    axes = {}

    overall = ['A', 'B', 'C']
    names = [ ['a', *[str(i + j * subcol) for i in range(subcol)], 'b', 'c'] for j in range(subrow)]

    for i in range(row):

    subfig : plt.Figure = subfigs

    mosaic = [[ overall + name for name in namerow] for namerow in names]
    width_ratios = [ 1.0, 0.5, 0.5, 0.5, 1.0, 1.0]
    height_ratios = [ 1.0, 1.0, 1.0]

    axes |= subfig.subplot_mosaic(mosaic, width_ratios=width_ratios, height_ratios=height_ratios)

    for ax_name in axes:

    axes[ax_name].set_title(ax_name)



    pdf.savefig(fig, dpi=50)


    And I would get something like the following[​IMG]

    Notice how the three main rows should have identical ratios but clearly the smaller plots have been unexpectedly adjusted.

    This behavior seems to change with various parameters such as the overall fig size and what not, but I'm just wondering if there's a reliable way to prevent this from happening.

    Continue reading...

Compartilhe esta Página