69

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.

2
  • Are you rotating the label in the same notebook cell it was created? You need to be Commented Aug 6, 2015 at 15:05
  • No I wasn't, but now that I am getting this error: set_xticklabels() takes at least 2 arguments (1 given)
    – Laurennmc
    Commented Aug 6, 2015 at 15:07

5 Answers 5

65

You can rotate seaborn xticks like so:

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

plt.xticks(rotation=70)
plt.tight_layout()
2
  • 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
    Commented Aug 3, 2021 at 9: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 Commented May 3, 2022 at 11:01
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

6
  • 10
    Could also do plt.setp(by_school.get_xticklabels(), rotation=45) to save yourself a line of code :)
    – mwaskom
    Commented Aug 6, 2015 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
    Commented Aug 6, 2015 at 18:10
  • 5
    You could also use plt.xticks(rotation='vertical') if you imported matplotlib.pyplot as plt.
    – JHall651
    Commented Feb 9, 2019 at 4: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()] Commented Mar 23, 2020 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
    Commented Aug 3, 2021 at 9:32
49

Use the following code statement:

by_school.set_xticklabels(by_school.get_xticklabels(), 
                          rotation=90, 
                          horizontalalignment='right')
1
  • 5
    Also add horizontalalignment='right' so that the labels match the ticks.
    – 6infinity8
    Commented Nov 24, 2019 at 9:06
4

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
2
  • AttributeError: 'AxesSubplot' object has no attribute 'xticks' When this was tried to be reproduced. Any thoughts?
    – bmc
    Commented Apr 28, 2018 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
    Commented Apr 30, 2018 at 5:00
-5

This worked for me:

g.fig.autofmt_xdate()

Not the answer you're looking for? Browse other questions tagged or ask your own question.