307

I have a simple factorplot

import seaborn as sns g = sns.factorplot("name", "miss_ratio", "policy", dodge=.2, linestyles=["none", "none", "none", "none"], data=df[df["level"] == 2]) 

enter image description here

The problem is that the x labels all run together, making them unreadable. How do you rotate the text so that the labels are readable?

2

9 Answers 9

451

I had a problem with the answer by @mwaskorn, namely that

g.set_xticklabels(rotation=30) 

fails, because this also requires the labels. A bit easier than the answer by @Aman is to just add

plt.xticks(rotation=30) 
Sign up to request clarification or add additional context in comments.

7 Comments

You can also get the labels like so g.set_xticklabels(g.get_xticklabels(), rotation=30). Assign it to a variable if you want to suppress the output.
I think this is correct, when setting plt.xtics worked for me.
When I try this, my xticklabels are no longer aligned with the x ticks. Does anyone know why?
I recommend to also add plt.tight_layout(), so that the margins are adjusted to fit the text
g.set_xticklabels(rotation=30) works on FacetGrid now
|
285

You can rotate tick labels with the tick_params method on matplotlib Axes objects. To provide a specific example:

ax.tick_params(axis='x', rotation=90) 

1 Comment

This works but ax should be g for this example: g.tick_params(axis='x', rotation=90)
37

This is still a matplotlib object. Try this:

# <your code here> locs, labels = plt.xticks() plt.setp(labels, rotation=45) 

Comments

27

Any seaborn plots suported by facetgrid won't work with (e.g. catplot)

g.set_xticklabels(rotation=30) 

however barplot, countplot, etc. will work as they are not supported by facetgrid. Below will work for them.

g.set_xticklabels(g.get_xticklabels(), rotation=30) 

Also, in case you have 2 graphs overlayed on top of each other, try set_xticklabels on graph which supports it.

1 Comment

There doesn't seem to be .get_* methods in FacetGrid (seaborn 0.13.2)
14

You can also use plt.setp as follows:

import matplotlib.pyplot as plt import seaborn as sns plot=sns.barplot(data=df, x=" ", y=" ") plt.setp(plot.get_xticklabels(), rotation=90) 

to rotate the labels 90 degrees.

Comments

13

If anyone wonders how to this for clustermap CorrGrids (part of a given seaborn example):

import seaborn as sns import matplotlib.pyplot as plt sns.set(context="paper", font="monospace") # Load the datset of correlations between cortical brain networks df = sns.load_dataset("brain_networks", header=[0, 1, 2], index_col=0) corrmat = df.corr() # Set up the matplotlib figure f, ax = plt.subplots(figsize=(12, 9)) # Draw the heatmap using seaborn g=sns.clustermap(corrmat, vmax=.8, square=True) rotation = 90 for i, ax in enumerate(g.fig.axes): ## getting all axes of the fig object ax.set_xticklabels(ax.get_xticklabels(), rotation = rotation) g.fig.show() 

Comments

8

For a seaborn.heatmap, you can rotate these using (based on @Aman's answer)

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 

Comments

6

If the labels have long names it may be hard to get it right. A solution that worked well for me using catplot was:

import matplotlib.pyplot as plt fig = plt.gcf() fig.autofmt_xdate() 

2 Comments

this solution not only rotates the labels but also makes sure that the x-offset is correct. With the accepted answer the labels are centered under the tick, but when rotated you want to have the end of the label at the tick location.
this answer actually makes positioning after rotation much better than other answers. Thank you!
1

Use ax.tick_params(labelrotation=45). You can apply this to the axes figure from the plot without having to provide labels. This is an alternative to using the FacetGrid if that's not the path you want to take.

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.