残念ながら、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"