Rotate label text in seaborn
Asked Answered
L

9

290

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?

Librarianship answered 24/10, 2014 at 1:15 Comment(2)
This helped me: #74716267Schumacher
For seaborn.objects with v0.12, see How to rotate the xticks with seaborn.objectsAnticipant
S
276

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)
Strawser answered 24/10, 2014 at 3:2 Comment(1)
This works but ax should be g for this example: g.tick_params(axis='x', rotation=90)Osburn
W
436

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=45)
Wildebeest answered 11/1, 2016 at 13:2 Comment(7)
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.Mon
I think this is correct, when setting plt.xtics worked for me.Butene
tried all the options above for a violin plot and plt.xticks(rotation=n) is only one I could get to workSuspensoid
When I try this, my xticklabels are no longer aligned with the x ticks. Does anyone know why?Lotta
I recommend to also add plt.tight_layout(), so that the margins are adjusted to fit the textTwobyfour
g.set_xticklabels(rotation=30) works on FacetGrid nowTwobyfour
g.tick_params('x', rotation=90) seems to work as of September 2022.Lacee
S
276

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)
Strawser answered 24/10, 2014 at 3:2 Comment(1)
This works but ax should be g for this example: g.tick_params(axis='x', rotation=90)Osburn
C
37

This is still a matplotlib object. Try this:

# <your code here>
locs, labels = plt.xticks()
plt.setp(labels, rotation=45)
Congruent answered 24/10, 2014 at 1:46 Comment(0)
C
23

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.

Crabwise answered 5/7, 2020 at 17:13 Comment(0)
S
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()
Spies answered 6/4, 2017 at 13:18 Comment(0)
V
13

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.

Vang answered 3/7, 2019 at 13:27 Comment(0)
Y
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
Yelena answered 31/10, 2017 at 16:55 Comment(0)
L
3

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()
Labe answered 29/3, 2021 at 9:4 Comment(0)
R
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.

Rimma answered 21/5, 2021 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.