2010-10-06 415 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