Spring
Spring

Reputation: 193

How to set rotation for seaborn FacetGrid and figure-level xtick labels

I have a dataframe (df) looks like below:

import pandas as pd
import seaborn as sns

data = {'Year': [2010, 2010, 2010, 2011, 2011, 2011, 2019, 2019, 2019],
        'Capacity': ['4,999 and under', '5,000-6,000', '6,100-7,000', '4,999 and under', '5,000-6,000', '6,100-7,000', '4,999 and under', '5,000-6,000', '6,100-7,000'],
        'Qty': [2, 1, 3, 5, 3, 3, 1, 3, 4]}

df = pd.DataFrame(data)

I want to generate subplots in a grid, one plot per year. Each plot has x='Capacity', y='Qty'.

g = sns.FacetGrid(df, col="Year", col_wrap=3, height=4, aspect=1)
g.map(sns.barplot, "BTU_Rating", "qty")

How do I add xtick label as listed in "Capacity" column?

Thank you for your help.

enter image description here

Upvotes: 4

Views: 2683

Answers (1)

Trenton McKinney
Trenton McKinney

Reputation: 62373

  • Specify g.set_xticklabels(rotation=x)

Updated Answer

import pandas as pd
import seaborn as sns

order = ['4,999 and under', '5,000-6,000', '6,100-7,000']
g = sns.catplot(data=df, col='Year', x='Capacity', y='Qty', order=order, kind='bar', col_wrap=3, height=4, aspect=1)
g.set_xticklabels(rotation=90)

enter image description here

Original Answer

import pandas as pd
import seaborn as sns

g = sns.FacetGrid(df, col="Year", col_wrap=3, height=4, aspect=1)

order = ['4,999 and under', '5,000-6,000', '6,100-7,000']
g.map(sns.barplot, "Capacity", "Qty", order=order)

g.set_xticklabels(rotation=90)

enter image description here

Upvotes: 4

Related Questions