Plot a time series grouped by id

 ̄綄美尐妖づ 提交于 2021-02-10 06:15:18

问题


I want to plot a time series grouped by id. So that time is my x-value and 'value' is my y-value. How can I plot x and y grouped by 'id' 1?

id   time  value 
1    1     0.3
1    2     0.6
1    3     0.9
2    1     0.1
2    2     0.3
2    3     0.6
3    1     0.2
3    2     0.4
3    3     0.5

回答1:


I think you can use pivot with DataFrame.plot.bar or only DataFrame.plot:

import matplotlib.pyplot as plt

df = df.pivot(index='time', columns='id', values='value')
print (df)
id      1    2    3
time               
1     0.3  0.1  0.2
2     0.6  0.3  0.4
3     0.9  0.6  0.5

df.plot.bar()
plt.show()

df = df.pivot(index='time', columns='id', values='value')
print (df)
id      1    2    3
time               
1     0.3  0.1  0.2
2     0.6  0.3  0.4
3     0.9  0.6  0.5

df.plot()
plt.show()



来源:https://stackoverflow.com/questions/40724135/plot-a-time-series-grouped-by-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!