1

I'm creating subplots. I would like to set the same xtick for all subplots. I was able to set the xlabel in common for all subplots but I really don't know how to do for xticks. Any help?

fig, axs = plt.subplots(2, 2)
axs[0,0].plot(np.float64(datatime),np.float64(Tm),'--',color='black')
axs[0,0].set_ylim([min(Tm)-10,max(Tm)+10])
axs[0,0].set_ylabel('Temp. [°C]')

axs[0,1].plot(np.float64(datatime),np.float64(precip),'--',color='black')
axs[0,1].set_ylim([min(precip),max(precip)+20])
axs[0,1].set_ylabel('Rainfall [mm]')

axs[1,0].plot(np.float64(datatime),np.float64(PET),color='magenta')
axs[1,0].set_ylim([min(PET),max(PET)+10])
axs[1,0].set_ylabel('PET [mm]')

axs[1,1].plot(np.float64(datatime),np.float64(delta),color='cyan')
axs[1,1].set_ylim([min(delta),max(delta)+10])
axs[1,1].set_ylabel('P-PET [mm]')

plt.xticks(np.arange(min(datatime), max(datatime)+1, 12))   #here i define xticks

for ax in axs.flat:
    ax.set(xlabel='Time [months]')

plt.show()
Tonechas
  • 13,398
  • 16
  • 46
  • 80
Elisa m
  • 135
  • 8
  • https://stackoverflow.com/questions/12608788/changing-the-tick-frequency-on-x-or-y-axis-in-matplotlib You can set a range and use that same range for all your subplots. – swiss_knight May 11 '20 at 09:31

1 Answers1

1

Within the for loop you can set the same ticks for each subplot

for ax in axs.flat:
    ax.set(xlabel='Time [months]')
    ax.set_xticks(np.arange(min(datatime), max(datatime)+1, 12))
Sheldore
  • 37,862
  • 7
  • 57
  • 71