2010-10-06 416 views
0

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

我想画一个图表,显示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 

-David

回答

2

尝试:

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轴的最佳格式。

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

+0

我是工作中心,但快速测试: – davidlt 2010-10-06 12:13:49