【问题标题】:Python drawing cumulative plot (matplotlib)Python绘制累积图(matplotlib)
【发布时间】:2010-10-06 06:08:08
【问题描述】:

我没有使用 matplotlib,但看起来它是绘图的主要库。我想绘制 CPU 使用情况图。我每分钟都有后台进程记录(日期,min_load,avg_load,max_load)。日期可以是时间戳或格式良好的日期。

我想绘制在同一个图上显示 min_load、avg_load 和 max_load 的图表。在 X 轴上,我想根据有多少数据来放置分钟、小时、天、周。

可能存在差距。假设受监控的进程崩溃,并且由于没有人重新启动它,因此可能会出现几个小时的间隙。

我的想象示例:http://img714.imageshack.us/img714/2074/infoplot1.png 这并不能说明差距,但在这种情况下,读数会变为 0。

我现在正在使用 matplotlib,我也会尝试分享我的结果。数据可能如下所示:

1254152292;0.07;0.08;0.13
1254152352;0.04;0.05;0.10
1254152412;0.09;0.10;0.17
1254152472;0.28;0.29;0.30
1254152532;0.20;0.20;0.21
1254152592;0.09;0.12;0.15
1254152652;0.09;0.12;0.14
1254152923;0.13;0.12;0.30
1254152983;0.13;0.25;0.32

Or it could look something like this:
Wed Oct 06 08:03:55 CEST 2010;0.25;0.30;0.35
Wed Oct 06 08:03:56 CEST 2010;0.00;0.01;0.02
Wed Oct 06 08:03:57 CEST 2010;0.00;0.01;0.02
Wed Oct 06 08:03:58 CEST 2010;0.00;0.01;0.02
Wed Oct 06 08:03:59 CEST 2010;0.00;0.01;0.02
Wed Oct 06 08:04:00 CEST 2010;0.00;0.01;0.02
Wed Oct 06 08:04:01 CEST 2010;0.25;0.50;0,75
Wed Oct 06 08:04:02 CEST 2010;0.00;0.01;0.02

-大卫

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    试试:

    from matplotlib.dates import strpdate2num, epoch2num
    import numpy as np
    from pylab import figure, show, cm
    
    datefmt = "%a %b %d %H:%M:%S CEST %Y"
    datafile = "cpu.dat"
    
    def parsedate(x):
        global datefmt
        try:
            res = epoch2num( int(x) )
        except:
            try:
                res = strpdate2num(datefmt)(x)
            except:
                print("Cannot parse date ('"+x+"')")
                exit(1)
        return res
    
    # parse data file
    t,a,b,c = np.loadtxt(
        datafile, delimiter=';',
        converters={0:parsedate},
        unpack=True)
    
    fig = figure()
    ax = fig.add_axes((0.1,0.1,0.7,0.85))
    # limit y axis to 0
    ax.set_ylim(0);
    
    # colors
    colors=['b','g','r']
    fill=[(0.5,0.5,1), (0.5,1,0.5), (1,0.5,0.5)]
    
    # plot
    for x in [c,b,a]:
        ax.plot_date(t, x, '-', lw=2, color=colors.pop())
        ax.fill_between(t, x, color=fill.pop())
    
    # legend
    ax.legend(['max','avg','min'], loc=(1.03,0.4), frameon=False)
    
    fig.autofmt_xdate()
    show()
    

    这会解析“cpu.dat”文件中的行。日期由parsedate 函数解析。

    Matplotlib 应该找到 x 轴的最佳格式。

    编辑:添加了 legend 和 fill_between(也许有更好的方法来做到这一点)。

    【讨论】:

      猜你喜欢
      • 2021-11-04
      • 2018-03-10
      • 1970-01-01
      • 2011-03-03
      • 2017-01-10
      • 2018-05-05
      • 2019-12-29
      • 2018-11-26
      • 2016-08-06
      相关资源
      最近更新 更多