3

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
matthew
  • 399
  • 1
  • 7
  • 15

1 Answers1

8

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()

graph

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()

graph

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252