Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib: Tick labels disappeared after set sharex in subplots [duplicate]

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.

like image 920
Heyu L Avatar asked Jul 27 '18 08:07

Heyu L


1 Answers

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()
like image 172
Mathieu Avatar answered Oct 16 '22 08:10

Mathieu