如何在Python中绘制这种类型的图形
我有这样一个数据框:如何在Python中绘制这种类型的图形,python,pandas,matplotlib,Python,Pandas,Matplotlib,我有这样一个数据框: Hour Type Value 1 1 63 1 2 52.1 1 5 53.8 1 4 92.1 1 3 1.4 1 6 4.1 1 2 0 2 1 8.1 2
Hour Type Value
1 1 63
1 2 52.1
1 5 53.8
1 4 92.1
1 3 1.4
1 6 4.1
1 2 0
2 1 8.1
2 2 6.1
2 6 0.6
2 5 0.1
2 4 1.09
3 1 9.5
... ... ...
23 3 22.5
我想画一幅这样的图
让我解释一下:
我想计算每小时的平均值,这可以通过
meanValuePerHour=df.groupby('hour')['Value'].agg('mean')
rpmByHour.plot.bar(x='Hour',y='Mean Value',title='Mean Valueby Hour')代码>
但是,现在,我想添加Type
变量。你看到每个酒吧有不同的颜色吗?这就是我也希望分组方式类型
像这样的
df.groupby(['Hour','Type'])['Value'].agg('mean')
IIUC,带有plot(kind='bar',stacked=True)
是您想要的:
df.groupby(['Hour', 'Type'])['Value'].mean().unstack().plot(kind='bar', stacked=True)
输出:
IIUC,带有绘图(kind='bar',stacked=True)
是您想要的:
df.groupby(['Hour', 'Type'])['Value'].mean().unstack().plot(kind='bar', stacked=True)
输出: