简体   繁体   English

在 Pandas seaborn 中制作堆积条形图

[英]Making a stacked bar chart in pandas seaborn

I have the following dataframe:我有以下数据框:

在此处输入图片说明

I want to create a stacked bar graph where the x-axis is the provider, y-axis is the percentage and the stacks are the visit number.我想创建一个堆叠条形图,其中 x 轴是提供者,y 轴是百分比,堆栈是访问次数。 The visit number may change for example gastro may have percents up to 10 visits but pediatric may have 7 visits.就诊次数可能会发生变化,例如胃可能有多达 10 次就诊的百分比,但儿科可能有 7 次就诊。 I am not sure how to create the stacked graphs.我不确定如何创建堆叠图。 This is the code I am using currently:这是我目前使用的代码:

sns.barplot(x="provider", y="percentage",
                col="visit_number", data=df[df['visit_number'] == 1], kind="bar", palette=palette)

However, I get a separate graph based on visit number but I want it in one stacked graph.但是,我根据访问次数获得了一个单独的图表,但我希望它在一个堆叠图中。 Please advise.请指教。

Try:尝试:

df.set_index(['provider','visit_number'])\
    ['percentage'].unstack()\
    .plot.bar(stacked=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM