条形图与groupby [英] Bar plot with groupby

查看:101
本文介绍了条形图与groupby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的分类变量case_satus具有四个唯一值.我拥有2014年至2016年的数据.我想按年份分组绘制case_status的分布.我尝试使用以下方法:

My categorical variable case_satus takes on four unique values. I have data from 2014 to 2016. I would like to plot the distribution of case_status grouped by year. I try to this using:

df.groupby('year').case_status.value_counts().plot.barh()

然后我得到以下情节:

我想拥有一个更好的代表.例如,我每年有一种颜色,而所有"DENIED"将彼此相邻.

What I would like to have is a nicer represenation. For example where I have one color for each year, and all the "DENIED" would stand next to each other.

我认为可以实现,因为groupby对象是一个多索引,但是我对它的理解不够深刻,无法创建所需的绘图.

I think it can be achieved since the groupby object is a multi-index, but I don't understand it well enough to create the plot I want.

解决方案是:

df.groupby('year').case_status.value_counts().unstack(0).plot.barh()

并得出

推荐答案

我认为您需要添加也可能是更改级别:

df.groupby('year').case_status.value_counts().unstack(0).plot.barh()

这篇关于条形图与groupby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆