问题
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