[Python] Remove (sub)plot, but keep axis label in matplotlib

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    I want to create a subplot plot in matplotlib with, say, 2 rows and 2 columns, but I only have 3 things to plot and want to keep the lower left subplot empty. However, I still want there to be a y-axis label at that position, which is supposed to refer to the whole second row.

    This is my code so far:

    import matplotlib.pyplot as plt

    x = [0, 1]
    y = [2, 3]

    ax = plt.subplot2grid((2, 2), (0, 0))
    ax.plot(x, y)
    ax.set_ylabel('first row')

    ax = plt.subplot2grid((2, 2), (0, 1))
    ax.plot(x, y)

    ax = plt.subplot2grid((2, 2), (1, 0))
    ax.set_ylabel('second row')
    # ax.axis('off') <---- This would remove the label, too

    ax = plt.subplot2grid((2, 2), (1, 1))
    ax.plot(x, y)

    plt.show()


    I have tried using axis('off'), but that removes the label, too. (Same, if I move it one line up, i.e. above ax.set_ylabel('second row').

    So the result so far looks like this:

    [​IMG]

    I want the empty white box (not just its black bounding box or ticks and tick labels) to disappear. Is this possible and if yes, how can I achieve it?

    Continue reading...

Compartilhe esta Página