在玩完我的数据,分组和取消堆栈后,我得到了以下系列:
day type meat veg vegan
0 2017-07-02 Evening 62.0 11.0 16.0
1 2017-07-03 Afternoon 54.0 6.0 16.0
2 2017-07-03 Evening 1.0 0.0 0.0
3 2017-07-04 Morning 26.0 2.0 2.0
4 2017-07-04 Afternoon 47.0 5.0 7.0
5 2017-07-04 Evening 46.0 7.0 14.0
6 2017-07-05 Morning 16.0 4.0 9.0
7 2017-07-05 Afternoon 36.0 9.0 24.0
8 2017-07-05 Evening 44.0 6.0 20.0
9 2017-07-06 Afternoon 47.0 8.0 16.0
10 2017-07-06 Evening 36.0 6.0 18.0
11 2017-07-07 Morning 36.0 4.0 8.0
12 2017-07-07 Afternoon 23.0 3.0 5.0
13 2017-07-07 Evening 50.0 12.0 16.0
14 2017-07-08 Morning 21.0 0.0 9.0
15 2017-07-08 Afternoon 21.0 2.0 7.0
16 2017-07-08 Evening 48.0 8.0 16.0
现在我想做的是绘制一个堆积的条形图,在y轴上绘制总数,在x轴上绘制膳食(晚上,下午,早晨),按天分组。
目前我管理的最好的是
.plot(kind='bar',stacked=True,rot=60)
这给了我一个堆积的情节,但有关膳食的信息丢失了。理想情况下,我希望:
| | | | | | -------------------------------------- .... Mor Aft Eve Mor Aft Eve .... ----------- ----------- 01/07 02/07
更新1
从Multiple stacked bar plot with pandas获得灵感 分别策划晚上,早晨和下午我得到的更好的东西仍然不尽如人意。
fig, ax = plt.subplots()
gdf.loc[gdf['day'] == 'Morning'].plot(kind='bar',stacked=True,rot=60,ax=ax, position=1.5, width=0.1)
gdf.loc[gdf['day'] == 'Afternoon'].plot(kind='bar',stacked=True,rot=60,ax=ax, position=0.5, width=0.1)
gdf.loc[gdf['day'] == 'Evening'].plot(kind='bar',stacked=True,rot=60,ax=ax, position=-0.5, width=0.1)
plt.show()
我想这可以更优雅地完成,无需绘制单独的子图,然后将它们拼接在一起
更新2
重新索引数据我可以得到更好的情节,但仍然不是我的目标,即分组标签
| | | | | | -------------------------------------- .... Mor Aft Eve Mor Aft Eve .... ----------- ----------- 01/07 02/07