66

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: Rotate label text in seaborn factorplot but the solution is not working.

Community
  • 1
  • 1
Laurennmc
  • 679
  • 1
  • 5
  • 7

5 Answers5

63

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 Rotate label text in seaborn factorplot

Community
  • 1
  • 1
CT Zhu
  • 52,648
  • 17
  • 120
  • 133
  • 10
    Could also do `plt.setp(by_school.get_xticklabels(), rotation=45)` to save yourself a line of code :) – mwaskom Aug 06 '15 at 18:08
  • 1
    Also `set_xticklabels` doesn't take positions (unlike `plt.xticks`, an example of an annoying inconsistency in the state machine/object oriented APIs) but the original code didn't work because OP was just passing a single string not a list of labels. – mwaskom Aug 06 '15 at 18:10
  • 5
    You could also use `plt.xticks(rotation='vertical')` if you imported matplotlib.pyplot as plt. – JHall651 Feb 09 '19 at 04:14
  • To make the answer a one-liner (if it's important to save one line): `[item.set_rotation(45) for item in by_school.get_xticklabels()]` – Magnus Persson Mar 23 '20 at 19:30
  • This works for me although if I include a title in the seaborn plot, i.e. `sns.barplot(...).set_title('My title')` , I get AttributeError: 'Text' object has no attribute 'get_xticklabels'. Is there a way around it? – Dudelstein Aug 03 '21 at 09:32
  • @MagnusPersson please do not use list comprehensions when you do not require the output. They should be reserved for list generation, and not for set methods. – Tobias P. G. Oct 05 '22 at 16:59
62

You can rotate seaborn xticks like so:

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

plt.xticks(rotation=70)
plt.tight_layout()
wordsforthewise
  • 13,746
  • 5
  • 87
  • 117
  • This does not work for me - specifically the plt.xticks overwrites the barplot with an empty one (with rotated ticks). Should I do something differently? I use Python 3.7 and Spyder. – Dudelstein Aug 03 '21 at 09:24
  • 1
    @Dudelstein maybe try upgrading your packages to make sure you're on the latest versions. I suspect some older packages or package version mismatches. Also check that you're using the proper installed package you think you are with `import seaborn as sns`; `sns.__file__` and `sns.__version__` for example. If you have multiple Python installations you can sometimes use different packages. from different installations accidentally – wordsforthewise May 03 '22 at 11:01
49

Use the following code statement:

by_school.set_xticklabels(by_school.get_xticklabels(), 
                          rotation=90, 
                          horizontalalignment='right')
Amit Vikram Singh
  • 2,090
  • 10
  • 20
3

If you come here to rotate the labels for a seaborn.heatmap, the following should work (based on @Aman's answer at 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
serv-inc
  • 35,772
  • 9
  • 166
  • 188
  • `AttributeError: 'AxesSubplot' object has no attribute 'xticks'` When this was tried to be reproduced. Any thoughts? – bmc Apr 28 '18 at 23:02
  • @bmc: Which `matplotlib.__version__` were you using? For version `2.0.0`, `plt.xticks()` worked: ``import seaborn as sns; import pandas as pd; import numpy as np; import matplotlib.pyplot as plt; df = pd.DataFrame(np.eye(3), range(3), range(3)); hm = sns.heatmap(df); plt.xticks(); `` – serv-inc Apr 30 '18 at 05:00
-5

This worked for me:

g.fig.autofmt_xdate()
HMD
  • 2,202
  • 6
  • 24
  • 37