pythonmatplotlibmultiple-axes

Put shared axis labels to upper plot


from matplotlib import pyplot as plt
fig, (ax0, ax1) = plt.subplots(nrows=2, sharex=True)
fig.show()

Returns this figure:

Current

But I want the x-axis labels below the first plot, not the second, like shown below. How can I achieve this?

Desired


Solution

  • The answer from @r-beginners brought me to a solution that also works when using the plt.subplots shortcut instead of instantiating each axis separately.

    from matplotlib import pyplot as plt
    fig, (ax0, ax1) = plt.subplots(nrows=2, sharex=True)
    plt.tick_params('x', labelbottom=False, labeltop=True)
    fig.show()
    

    he essential part is plt.tick_params which take keyword arguments labeltop or labelbottom (as well as labelleft or labelright for shared axis on several columns) to select / deselect each axis individually.