如何使用gnuplot绘制时间序列中的直方图 [英] How to plot histogram in timeseries with gnuplot
问题描述
我是gnuplot的新手,希望能帮助我了解如何绘制时间序列直方图
I am new to gnuplot and I would appreciate some help to understand how I can plot a timeseries histogram
我的数据如下:
#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天的数据.
我需要绘制以下图表:
- 直方图中x分钟(例如5分钟)的WKB平均值
- 直方图中x分钟(例如5分钟)的WKB累积总和
这是我当前的脚本:
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
我确定我错过了很多事情.:)
I am sure I am missing a lot of things. :)
推荐答案
不幸的是,gnuplot并不适合处理此类数据处理任务.您可能可以提出一个解决方案,但这将非常混乱并且难以使用.幸运的是,gnuplot可以从其他程序中读取管道-因此,最简单的解决方案是编写一个简单的脚本来处理输入数据并将其写入标准输出.我会选择python:
Unfortunately, gnuplot just isn't well suited to handle data processing tasks such as these. You probably could come up with a solution, but it would be extremely messy and super hard to use. Fortunately, gnuplot can read from pipes from other programs -- So the easiest solution is to write a simple script to process the input data and write it to standard output. I'd choose 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
这会将您的数据文件(在示例数据上运行)格式化为:
This will format your datafile (run on your sample data) as:
2013-02-06 11:45:00 5029.5 30177.0
2013-02-06 11:55:00 5029.5 30177.0
可以用gnuplot中的框来绘制:
which can be plotted with boxes in 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"
这篇关于如何使用gnuplot绘制时间序列中的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!