Rotate tick labels for seaborn barplot

PythonMatplotlibSeaborn

Python Problem Overview


I am trying to display a chart with rotated x-axis labels, but the chart is not displaying.

import seaborn as sns
%matplotlib inline

yellow='#FFB11E'
by_school=sns.barplot(x ='Organization Name',y ='Score',data = combined.sort('Organization Name'),color=yellow,ci=None)

At this point I can see the image, but after I set the xticklabel, I don't see the image anymore only an object reference. (I would post the image, but I don't enough reputation :()

by_school.set_xticklabels('Organization Name',rotation=45)

<matplotlib.axes._subplots.AxesSubplot at 0x3971a6a0>

A similar question is posted here: https://stackoverflow.com/questions/26540035/rotate-label-text-in-seaborn-factorplot but the solution is not working.

Python Solutions


Solution 1 - Python

You need a different method call, namely .set_rotation for each ticklables. Since you already have the ticklabels, just change their rotations:

for item in by_school.get_xticklabels():
    item.set_rotation(45)

barplot returns a matplotlib.axes object (as of seaborn 0.6.0), therefore you have to rotate the labels this way. In other cases, when the method returns a FacetGrid object, refer to https://stackoverflow.com/questions/26540035/rotate-label-text-in-seaborn-factorplot?lq=1

Solution 2 - Python

You can rotate seaborn xticks like so:

sns.barplot(x='Organization Name', y='Score', data=df)

plt.xticks(rotation=70)
plt.tight_layout()

Solution 3 - Python

Use the following code statement:

by_school.set_xticklabels(by_school.get_xticklabels(), 
                          rotation=90, 
                          horizontalalignment='right')

Solution 4 - Python

If you come here to rotate the labels for a seaborn.heatmap, the following should work (based on @Aman's answer at https://stackoverflow.com/questions/26540035/rotate-label-text-in-seaborn-factorplot)

pandas_frame = pd.DataFrame(data, index=names, columns=names)
heatmap = seaborn.heatmap(pandas_frame)
loc, labels = plt.xticks()
heatmap.set_xticklabels(labels, rotation=45)
heatmap.set_yticklabels(labels[::-1], rotation=45) # reversed order for y

Solution 5 - Python

This worked for me:

g.fig.autofmt_xdate()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLaurennmcView Question on Stackoverflow
Solution 1 - PythonCT ZhuView Answer on Stackoverflow
Solution 2 - PythonwordsforthewiseView Answer on Stackoverflow
Solution 3 - PythonAmit Vikram SinghView Answer on Stackoverflow
Solution 4 - Pythonserv-incView Answer on Stackoverflow
Solution 5 - PythonKairat SharshenbaevView Answer on Stackoverflow