不幸的是,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"
感謝您的Python程序我會給它一試 – wantro