GNU Plot how to plot histogram in timeseries

霸气de小男生 提交于 2021-02-11 15:02:28

问题


I am new to gnuplot and I would appreciate some help to understand how I can plot a timeseries histogram

My data looks like this:

#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 
...................

I have data around 2 days.

What I need is to plot following charts:

  1. Average of WKB for x minues (for e.g. 5 minutes) in histogram
  2. Cumulative sum of WKB for x minues (for e.g. 5 minutes) in histogram

This my current script:

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. :)


回答1:


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

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"

or

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


来源:https://stackoverflow.com/questions/14770053/gnu-plot-how-to-plot-histogram-in-timeseries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!