Home > Mobile >  Put shared axis labels to upper plot
Put shared axis labels to upper plot

Time:12-25

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

CodePudding user response:

There is an example in the enter image description here

CodePudding user response:

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.

  • Related