Osca
Osca

Reputation: 1694

Plot stacked barplot with seaborn catplot

I wonder is it possible to plot stacked bar plot with seaborn catplot. For example:

import seaborn as sns
exercise = sns.load_dataset("exercise")
plot = exercise.groupby(['diet'])['kind'].value_counts(normalize=True).mul(100).reset_index(name='percentage%')
g = sns.catplot(x="diet", y="percentage%", hue="kind", data=plot, kind='bar')

enter image description here

I'd like to stack kind, but it seems catplot doesn't take 'stacked' parameter.

Upvotes: 4

Views: 7935

Answers (1)

StupidWolf
StupidWolf

Reputation: 46888

You cannot do it using sns.barplot, i think the closest you can get is using sns.histplot:

import seaborn as sns
exercise = sns.load_dataset("exercise")
plot = exercise.groupby(['diet'])['kind'].value_counts(normalize=True).mul(100).reset_index(name='percentage')
g = sns.histplot(x = 'diet' , hue = 'kind',weights= 'percentage',
             multiple = 'stack',data=plot,shrink = 0.7)

enter image description here

Upvotes: 3

Related Questions