【问题标题】:Pandas matplotlib graphing multiple subplotsPandas matplotlib 绘制多个子图
【发布时间】:2018-07-12 20:21:59
【问题描述】:

我正在尝试创建一个图表,在 y 轴上绘制事件计数,在 x 轴上绘制时间。我想要两个不同的子图,每个子图代表一个唯一的进程 ID(以监视该进程如何影响 event_count)。这是我的一些数据:

  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 实现这一点的任何想法?

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    您可以使用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()
    

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

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

    或者更简单,使用pandas 内置绘图(基于matplotlib):

    fig, ax = plt.subplots()
    df.set_index('Time').groupby('ProcessID')['event_count'].plot(ax=ax, legend=True)
    
    plt.show()
    

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

    【讨论】:

    • 太棒了!很抱歉没有在问题中指定,但您知道如何使两个图表在同一个图上吗?
    • 在您的问题中,您要求提供子图,但请参阅我的编辑以另一种方式:将两个图一起显示 IMO 确实提供更多信息
    猜你喜欢
    • 2018-06-27
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    • 2023-03-24
    • 2015-01-09
    • 2018-11-26
    • 2022-08-16
    • 1970-01-01
    相关资源
    最近更新 更多