Benjamin Doughty
Benjamin Doughty

Reputation: 475

Is there an easy way to plot two categorical plots on the same FacetGrid in Seaborn?

I am attempting to use Seaborn's catplot functionality to plot barplots, faceted by two features.

sns.catplot(kind='bar', data=df, col='col1', y='col2', x='col3')

I now want to add the original data (in the form of a stripplot) on top. If I were doing this with only one panel, I would just plot them on the same axes:

fig, ax = plt.subplots()
sns.barplot(data=df, y='col2', x='col3', ax=ax, alpha=0.5)
sns.stripplot(data=df, y='col2', x='col3', ax=ax, dodge=True)

matplotlib axes with barplot and stripplot

However, since catplot returns a FacetGrid, I cannot figure out how to do the analogous thing. Thanks!

Upvotes: 0

Views: 447

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40747

catplot is a helper function designed to facilitate common faceted plots. If you want more control, you are advised to use FacetGrid directly:

N = 1000
df = pd.DataFrame({'col1':np.random.choice(['A','B','C'], size=N),
                   'col2':np.random.random(size=N),
                   'col3':np.random.choice(['D','E'], size=N)})
g = sns.FacetGrid(data=df, col='col1', col_wrap=2)
g.map_dataframe(sns.barplot, x='col3', y='col2', alpha=0.5, ci=None)
g.map_dataframe(sns.stripplot, x='col3', y='col2')

enter image description here

Upvotes: 2

Related Questions