用seaborn catplot绘制堆叠条形图

2024-04-28 20:21:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道是否可以用seaborn catplot绘制堆叠条形图。 例如:

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

我想堆叠kind,但catplot似乎不接受'stacked'参数


Tags: importplotvalueas绘制loadseaborndataset
1条回答
网友
1楼 · 发布于 2024-04-28 20:21:54

你不能使用sns.barplot,我认为最接近的方法是使用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

相关问题 更多 >