【发布时间】:2019-10-15 12:41:44
【问题描述】:
背景:从大的DataFrame 中过滤出year=2013、month=June、第 3 周至第 9 周(周一至周日)的条目。然后,我按day、hour 和user_type 对数据进行分组,并旋转表格得到DataFrame,如下所示:
Day Hour Casual Registered Casual_percentage
0 3 0 14 19 42.42
1 3 1 8 8 50.00
2 3 2 1 3 25.00
3 3 3 2 1 66.67
4 3 4 1 3 25.00
5 3 5 1 17 5.56
. . . . . .
我每天都有 24 小时,所以对于第 4 天(星期二),数据开始如下:
. . . . . .
21 3 21 32 88 26.67
22 3 22 26 64 28.89
23 3 23 23 30 43.40
24 4 0 10 11 47.62
25 4 1 1 5 16.67
26 4 2 1 1 50.00
. . . . . .
如何为每个Hour 绘制Casual 和Registered 变量,为7 个Days 中的每一个?我需要创建 7 个不同的图并将它们对齐在 1 个图中吗?
当前代码。我觉得我已经走投无路了。我还尝试使用documentation 创建一个second x-axis(用于Days)。
def make_patch_spines_invisible(ax):
ax.set_frame_on(True)
ax.patch.set_visible(False)
for sp in ax.spines.values():
sp.set_visible(False)
fig, ax1 = plt.subplots(figsize=(10, 5))
ax1.set(xlabel='Hours', ylabel='Total # of trips started')
ax1.plot(data.Hour, data.Casual, color='g')
ax1.plot(data.Hour, data.Registered, color='b')
"""This part is trying to create the 2nd x-axis (Days)"""
ax2 = ax1.twinx()
#offset the bottom spine
ax2.spines['bottom'].set_position(('axes', -.5))
make_patch_spines_invisible(ax2)
#show bottomm spine
ax2.spines['bottom'].set_visible(True)
ax2.set_xlabel("Days")
plt.show()
【问题讨论】:
标签: python pandas matplotlib