How to rotate xticklabels in a seaborn catplot

11.3k Views Asked by At

I'm not able to rotate my xlabels in Seaborn/Matplotlib. I have tried many different solutions but not able to fix it. I have seen many related questions here on stackoverflow, but they have not worked for me.

My current plot looks like this, but I want the xlabels to rotate 90.

enter image description here

@staticmethod
def plotPrestasjon(plot):
    sns.set(style="darkgrid")
    ax = sns.catplot(x="COURSE", y="FINISH", hue="COURSE",
                     col="BIB#", data=plot, s=9, palette="Set2")
    ax.set(xlabel='COURSES', ylabel='FINISH (sec)')
    plt.show()

I have tried:

ax.set_xticklabels(ax.get_xticklabels(), rotation=90)

But that fails to generate the plot. Any ideas how I can fix it?

2

There are 2 best solutions below

1
On BEST ANSWER
  • The correct way to set the xticklabels for sns.catplot, according to the documentation, is with the .set_xticklabels method (.e.g. g.set_xticklabels(rotation=30)).
  • Using a loop to iterate through the Axes, should be used if changes need to be made on a plot by plot basis, within the FacetGrid.
  • Building structured multi-plot grids
  • seaborn.FacetGrid
  • g, or in the case of the OP, ax is a seaborn.axisgrid.FacetGrid
    • When iterating through ax.axes.flat, axes is a <class 'matplotlib.axes._subplots.AxesSubplot'>, which has a wide array of class methods, including .get_xticklabels().
  • In cases with many columns, and col_wrap= is used, g.set_xticklabels(rotation=30) may result in removing all the xticklabels.
    • Instead, labels= must be specified, g.set_xticklabels(g.axes.flat[-1].get_xticklabels(), rotation=30), where g.axes.flat[-1] should be the last facet axes with xticklabels.
    • Alternatively, use g.tick_params(axis='x', rotation=30)
  • Tested in python 3.12, matplotlib 3.8.1, seaborn 0.13.0

One Row & Multiple Columns

import seaborn as sns

# load data
exercise = sns.load_dataset("exercise")

# plot catplot
g = sns.catplot(x="time", y="pulse", hue="kind", col="diet", data=exercise)

# set rotation
g.set_xticklabels(rotation=30)

enter image description here

Multiple Rows & Columns

# plot catplot
g = sns.catplot(x="time", y="pulse", hue="diet", col="kind", col_wrap=2, data=exercise)

# set rotation using one of the following options
# g.set_xticklabels(labels=g.axes.flat[-1].get_xticklabels(), rotation=30)
g.tick_params(axis='x', rotation=30)

enter image description here


  • Using g.set_xticklabels(g.get_xticklabels(), rotation=30) results in an AttributeError.
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-442-d1d39d8cc4f0> in <module>
      1 g = sns.catplot(x="time", y="pulse", hue="kind", col="diet", data=exercise)
----> 2 g.set_xticklabels(g.get_xticklabels(), rotation=30)

AttributeError: 'FacetGrid' object has no attribute 'get_xticklabels'
0
On

Try iterating through the axes that belong to the FacetGrid:

for axes in ax.axes.flat:
    axes.set_xticklabels(axes.get_xticklabels(), 
                         rotation=90, 
                         horizontalalignment='right')
  • A FacetGrid is composed of a collection of Axes, and some of the methods that look so intuitive for the whole "plot", are actually meant to be used on each individual Axes. Search for Axes in the documentation. You'll see they often iterate through them when they need to change a feature that pertains to each individual subplot (as opposed to the whole plot's legend, for example).