[Python] Pyplot: Shared x-axis, how to shift individual ytick labels?

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

  1. Stack

    Stack Membro Participativo

    I would like to plot two datasets sharing the same x-axis, like so:

    import numpy as np
    import matplotlib.pyplot as plt

    diagramm,axes=plt.subplots(nrows=2,sharex='col',gridspec_kw={'hspace': 0, 'wspace': 0})
    axes[1].set_xlim([0,10])

    axes[0].set_ylim(-1.0,1.0)
    axes[1].set_ylim(-0.01,0.01)

    xval=np.linspace(0,10,100)

    axes[0].plot(xval,np.sin(xval), color="red")
    axes[1].plot(xval,0.01*np.sin(xval), color="blue")

    plt.show()


    The result is the following:

    [​IMG]

    You see the problem: The lowest/highest ytick labels of the upper/lower plot are overprinted on each other. How can I shift these labels up/down, so that they are both readable?

    (Note: Obviously, I could add hspace or change the limits to, e.g., -1.1 and 0.011. However, I would prefer shifting the tick labels, if possible.)

    Continue reading...

Compartilhe esta Página