1

I am kind of stuck with this, I searched on google for an answer but I can not find it.

I would like to turn my xticks 45 degrees of my subplot. I know how to do it for a normal plot:

plt.xticks(rotation = 45)

But when I put this in my plot script nothing happens. I would really like to know how I can apply this to all my subplots at the same time.

This is my code:

plt.figure()

fig, axs = plt.subplots(1, 3, sharey=True, tight_layout=True)
axs[0].set_title("Classic")
axs[1].set_title("Theta")
axs[2].set_title("Vector")


axs[0].plot(df.time,df.classic, label = 'Classic', color = 'blue')
axs[1].plot(df.time,df.theta, label = 'Theta', color = 'green')
axs[2].plot(df.time,df.vector, label = 'Vector', color = 'red')

plt.xticks(rotation = 45)
plt.suptitle('Core computations',fontsize=20)
plt.legend()
plt.show()
Cardstdani
  • 4,999
  • 3
  • 12
  • 31
SimonDL
  • 186
  • 10

2 Answers2

1

Have you tried this?

ax = plt.gca()
ax.tick_params(axis='x', labelrotation = 45)

For all subplots, you can also try:

for ax in fig.axes:
    matplotlib.pyplot.sca(ax)
    plt.xticks(rotation=90)
blunova
  • 2,122
  • 3
  • 9
  • 21
  • Thanks for the for loop! I can use this now for multiple changes! I can not accept your answer yet because its to fast =D – SimonDL Jun 29 '22 at 15:47
1

Try with setp() function to set a certain parameter, in this case rotation:

ax = plt.gca()
plt.setp(ax.get_xticklabels(), rotation=45)
Cardstdani
  • 4,999
  • 3
  • 12
  • 31