2013-02-08 26 views
0

我是新來GNUPLOT,我希望得到一些幫助,以瞭解如何可以繪製時間序列直方圖GNU情節如何繪製在時間序列直方圖

我的數據是這樣的:

#Date Time  WKB 
20130206 11:45:57 4544 
20130206 11:45:57 5113 
20130206 11:45:57 5117 
20130206 11:45:57 5123 
20130206 11:45:57 5129 
20130206 11:45:57 5151 
................... 

我有數據大約2天。

我需要的是繪製以下圖表:WKB的在直方圖X MINUES(對於例如5分鐘)

  1. 平均
  2. WKB對於x MINUES(對於例如5分鐘)的累計總和直方圖

這是我當前的腳本:

set xdata time 
set xtics 36000 
set timefmt "%Y%m%d %H:%M:%S" 
set format x "%Y-%m-%dT%H:%M:%S" 
plot using 1:3 title smooth cumulative 

我相信我失蹤很多東西。 :)

回答

1

不幸的是,gnuplot只是不太適合處理這些數據處理任務。你可能可能拿出一個解決方案,但它會非常混亂,超級難用。幸運的是,gnuplot可以從其他程序的管道讀取 - 所以最簡單的解決方案是編寫一個簡單的腳本來處理輸入數據並將其寫入標準輸出。我會選擇Python:

import time 
from datetime import datetime 
from collections import defaultdict 
import sys 

def datetime_2_epoch(dt): 
    return int(time.mktime(dt.timetuple())) 

def epoch_2_datetime(epoch): 
    return datetime.fromtimestamp(epoch) 

data = defaultdict(list) 
with open(sys.argv[1]) as fin: 
    for line in fin: #Parse file 1 line at a time 
     timestr,datastr = line.rsplit(None,1) 
     try: 
      dt = datetime.strptime(timestr,"%Y%m%d %H:%M:%S") 
      val = float(datastr) 
     except ValueError: #couldn't read this line. must be a comment or something. 
      continue 

     bin = datetime_2_epoch(dt)//300 #300 = 60*5 -- 5 minute bin size 
     data[bin].append(val) 

for bin,lst in sorted(data.items()): 
    cum_sum = sum(lst) 
    avg = cum_sum/len(lst) 
    print epoch_2_datetime(bin*300),avg,cum_sum 

這將格式化數據文件(在你的樣本數據運行)爲:

2013-02-06 11:45:00 5029.5 30177.0 
2013-02-06 11:55:00 5029.5 30177.0 

可與盒中的gnuplot繪製:

set xdata time 
set timefmt '%Y-%m-%d %H:%M:%S' 
set yrange [0:*] 
plot '<python test.py test.dat' u 1:3 w boxes title "5 minute average" 

plot '<python test.py test.dat' u 1:4 w boxes title "5 minute sum" 
+0

感謝您的Python程序我會給它一試 – wantro