Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 绘制多个子图的matplotlib_Python_Pandas_Matplotlib - Fatal编程技术网

Python 绘制多个子图的matplotlib

Python 绘制多个子图的matplotlib,python,pandas,matplotlib,Python,Pandas,Matplotlib,我试图创建一个图表,将事件计数绘制在y轴上,时间绘制在x轴上。我希望有两个不同的子批,每个子批表示一个唯一的进程id(以监视该进程如何影响事件计数)。以下是我的一些数据: ProcessID Time event_count 479 1592 1.49 62760 480 1592 1.49 379620 481 1592 1.49 117124 482

我试图创建一个图表,将事件计数绘制在y轴上,时间绘制在x轴上。我希望有两个不同的子批,每个子批表示一个唯一的进程id(以监视该进程如何影响事件计数)。以下是我的一些数据:

  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
到目前为止,我已经做到了这一点:

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

关于如何使用pandas matplotlib实现这一点,您有什么想法吗?

您可以使用
groupby
并遍历您的组。要显示有关数据的粗略示例,请执行以下操作:

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

编辑:要使两者在同一个绘图(而不是子绘图)上,您可以使用更简单的语法,因为您不必指定ax,将整个内容放在列表中,大致如下:

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

或者更简单地说,使用内置绘图(构建在matplotlib上):


就我个人而言,我觉得这很难定制

太棒了!很抱歉没有在问题中指定,但您知道如何使两个图位于同一个绘图上吗?在您的问题中,您要求提供子绘图,但另一方面请参见我的编辑:将两个绘图一起显示确实更具信息性
fig, ax = plt.subplots()
df.set_index('Time').groupby('ProcessID')['event_count'].plot(ax=ax, legend=True)

plt.show()