Search code examples
pythonmatplotlibvisible

matplotlib: Tick labels disappeared after set sharex in subplots


When use sharex or sharey in subplots, the tick labels would disappeared, how to turn them back? Here is an example just copied from the official website:

fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
axs[0, 0].plot(x)

plt.show()

And we will see: enter image description here

As we can see, the top-right plot doesn't have any tick labels, and others also lack some labels because of the axis was shared. I think I should use something like plt.setp(ax.get_xticklabels(), visible=True), but it doesn't work.


Solution

  • You can use the tick_params() to design the plot:

    f, ax = plt.subplots(2, 2, sharex=True, sharey=True)
    
    for a in f.axes:
        a.tick_params(
        axis='x',           # changes apply to the x-axis
        which='both',       # both major and minor ticks are affected
        bottom=True,
        top=False,
        labelbottom=True)    # labels along the bottom edge are on
    
    plt.show()