Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate existing axis tick labels in Matplotlib

I start with tree plots:

df = pd.DataFrame([1,20,3],[2,30,4],[3,40,5],columns=['mean','size','stat'])

fig,[ax1,ax2,ax3] = plt.subplots(1, 3, sharey=True)

ax1.barh(np.arange(len(df)),df['mean'].values, align='center')
ax2.barh(np.arange(len(df)),df['size'].values, align='center')
ax3.barh(np.arange(len(df)),df['stat'].values, align='center')

Is there a way to rotate the x axis labels on all three plots?

like image 765
Chris Avatar asked Oct 29 '25 17:10

Chris


2 Answers

When you're done plotting, you can just loop over each xticklabel:

for ax in [ax1,ax2,ax3]:
    for label in ax.get_xticklabels():
        label.set_rotation(90) 
like image 131
CPBL Avatar answered Oct 31 '25 06:10

CPBL


You can do it for each ax your are creating:

ax1.xaxis.set_tick_params(rotation=90)
ax2.xaxis.set_tick_params(rotation=90)
ax3.xaxis.set_tick_params(rotation=90)

or you do it inside a for before showing the plot if you are building your axs using subplots:

for s_ax in ax:
  s_ax.xaxis.set_tick_params(rotation=90)
like image 26
Rmobdick Avatar answered Oct 31 '25 06:10

Rmobdick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!