Reputation: 47
I am trying to create a graph that graphs event count on the y axis and time on the x axis. I want to have two different subplots, each representing a unique process id (to monitor how that process affects event_count). Here is some of my data:
ProcessID Time event_count
479 1592 1.49 62760
480 1592 1.49 379620
481 1592 1.49 117124
482 1592 2.62 450024
483 1592 2.62 126941
484 1592 3.75 126360
485 1592 3.76 468223
486 1592 4.88 400239
487 1592 4.88 129450
488 1592 6.01 441982
489 1592 6.01 129858
490 1592 7.14 88848
491 1592 7.14 421015
492 1592 7.14 125487
493 1592 8.27 427974
494 1592 8.27 131260
495 1592 9.40 441375
496 1592 9.40 129779
497 1592 10.53 414021
498 1592 10.53 131006
499 1592 11.66 434822
500 1592 11.66 128453
501 1592 12.79 51726
ProcessID Time event_count
52715 7908 1.49 95615
52716 7908 2.62 95974
52717 7908 3.75 95174
52718 7908 3.76 116662
52719 7908 4.88 74974
52720 7908 4.88 102559
52721 7908 6.01 74307
52722 7908 6.01 108027
52723 7908 7.14 110227
52724 7908 8.27 83922
This is what I have so far to get to this point:
df = pd.read_csv('Book3.csv')
df = df.rename(columns={'Time ': 'Time'})
df['Time'] = df.Time.astype(float)
df2 = df.ix[:,['ProcessID','Time', 'event_count']].query('ProcessID == 1592')
df3 = df.ix[:,['ProcessID','Time', 'event_count']].query('ProcessID == 7908')
Any ideas on how to achieve this using pandas matplotlib?
Upvotes: 1
Views: 92
Reputation: 51335
You can use groupby
and iterate through your groups. To show a rough example on your data:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2)
for subplot_number, (pID, data) in enumerate(df.groupby('ProcessID')):
axes[subplot_number].plot(data['Time'], data['event_count'])
axes[subplot_number].set_title('Process ID: {}'.format(pID))
axes[subplot_number].set_ylabel('Event Count')
axes[subplot_number].set_xlabel('Time')
plt.tight_layout()
plt.show()
Edit: To make both on the same plot (as opposed to subplots), you can use more simple syntax because you don't have to specify the ax, putting the whole thing in a list comprehension, something along the lines of:
[plt.plot(data.Time, data.event_count, label='Process ID: {}'.format(pID)) for pID, data in df.groupby('ProcessID')]
plt.ylabel('Event Count')
plt.xlabel('Time')
plt.legend(loc=1)
plt.tight_layout()
plt.show()
Or even more simple, use pandas
built in plot (which builds on matplotlib
):
fig, ax = plt.subplots()
df.set_index('Time').groupby('ProcessID')['event_count'].plot(ax=ax, legend=True)
plt.show()
Personally, I find this harder to customize
Upvotes: 1