4

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?

1
  • 1
    Might I suggest revising the question title? I like that "existing" is there, but having "ticklabels" or "xticklabels" or "tick" in the question title would help. I think "axis labels" are something completely different. Commented Oct 13, 2016 at 13:31

4 Answers 4

8

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) 
Sign up to request clarification or add additional context in comments.

Comments

5

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) 

Comments

2
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) plt.subplot(1,3,1) barh(np.arange(len(df)),df['mean'].values, align='center') locs, labels = xticks() xticks(locs, labels, rotation="90") plt.subplot(1,3,2) barh(np.arange(len(df)),df['size'].values, align='center') locs, labels = xticks() xticks(locs, labels, rotation="90") plt.subplot(1,3,3) barh(np.arange(len(df)),df['stat'].values, align='center') locs, labels = xticks() xticks(locs, labels, rotation="90") 

Should do the trick.

Comments

0

Here is another more generic solution: you can just use axes.flatten() which will provide you with much more flexibility when you have higher dimensions.

for i, ax in enumerate(axes.flatten()):

sns.countplot(x= cats.iloc[:, i], orient='v', ax=ax) for label in ax.get_xticklabels(): # only rotate one subplot if necessary. if i==3: label.set_rotation(90) 

fig.tight_layout()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.