How to display the x -axis text in a tilted/rotate to 45 degree in Python Seaborn Subplots?

Question:

I have the below the below three dataframes

df = pd.DataFrame(['JohnJohnJohnJohn','JuneJuneJuneJune','DanDanDanDan','JohnJohnJohnJohn','DanDanDanDan','DineDineDineDine','JuneJuneJuneJune','JohnJohnJohn'],columns =['Months'])
df1 = pd.DataFrame(['John_1John_1John_1','June_1June_1June_1','Dine_1Dine_1Dine_1','John_1John_1John_1','Dine_1Dine_1Dine_1','John_1John_1John_1'],columns =['Months'])
df2 = pd.DataFrame(['John_2John_2','Dine_2Dine_2','Dine_2Dine_2','John_2John_2','June_2June_2','John_2John_2'],columns =['Months'])

When displaying them in seaborn countplot, the x axis label gets over-write on each other. How can I rotate to 45 degree in display? I tried the below but doesn’t work

f, axes = plt.subplots(1, 3)

sns.countplot(x='Months', data=df,order=df.Months.value_counts().index,ax=axes[0]).set(title='1D')
sns.countplot(x='Months', data=df1,order=df1.Months.value_counts().index,ax=axes[1]).set(title='2D')
sns.countplot(x='Months', data=df2,order=df2.Months.value_counts().index,ax=axes[2]).set(title='3D')
# axes.set_xticklabels(axes.get_xticklabels(), rotation=40, ha="right")

Asked By: John

||

Answers:

You can rotate the labels for all axes:

for ax in axes:
    ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
Answered By: Tranbi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.