我已经在这个问题上徘徊了一段时间了,我想我应该发帖了。我有一个包含UserID、TimeSlot和Count的数据帧。
我尝试在单独的折线图上绘制两个用户it (3和4),沿x轴使用TimeSlot,在Y轴上计算总数。我尝试引用this excellent post,但似乎不能满足我的需要。
我如何修复这段代码?
userList = [3,4]
df.set_index('TimeSlot', inplace=True)
df2 = df.groupby('UserID')
ncols=2
nrows = int(np.ceil(df2.ngroups/ncols))
fig, axes = plt.subplots(nrows=4, ncols=ncols, figsize=(12,4), sharey=True)
for (key, ax) in zip(df2.groups.keys(), axes.flatten()):
df2.get_group(key).plot(ax=ax)
ax.legend()
plt.show()发布于 2017-06-18 03:47:57
好的,这是我的最终解决方案:
df.set_index('TimeSlot', inplace=True)
userList = [1,2,3]
df2 = df.loc[df['UserID'].isin(userList)]
df2 = df2.pivot(columns='UserID', values='Count')
df2.plot()它实际上在同一张图表上绘制了线条,我认为这是很好的。
https://stackoverflow.com/questions/44607113
复制相似问题