1

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

2 Answers 2

5

Even with gnuplot 4.6.0 (at the time of OP's question) it was/is "easily" possible to handle timeseries histograms with gnuplot only. Check the following example. It generates a file with random data (normal distributed times around now) and puts it into bins of 5 minutes. Check help smooth unique und help smooth frequency.

Script: (tested with gnuplot 4.6.0 (March 2012) and 5.4.1 (Dec. 2020)

### time data histograms
reset

FILE = "SO14770053.dat"

# create some random test data
set print FILE
    now = time(0)
    do for [i=1:1000] {
        print sprintf("%s %d", strftime("%Y%m%d %H:%M:%S",now+invnorm(rand(0))*3600), int(rand(0)*2000)+3000)
    }
set print

myBinWidth = 300   # 300 sec = 5 min
Bin(col)   = floor(timecolumn(col)/myBinWidth)*myBinWidth

set boxwidth myBinWidth absolute
set xdata time
set timefmt "%Y%m%d %H:%M:%S"
set format x "%d.%m.\n%H:%M"
set xtics out
set style fill transparent solid 0.3

set multiplot layout 2,1
    set ytics 500
    plot FILE u (Bin(1)):3 smooth unique w boxes lc rgb "red" ti "Average in 5 min bins"

    set ytics 50000
    plot FILE u (Bin(1)):3 smooth freq w boxes lc rgb "blue" ti "Sum in 5 min bins"
unset multiplot
### end of script

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

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"

2 Comments

Thanks for the python program I will give it a try
@mgilson I disagree! Even at that time you could "easily" handle such tasks with gnuplot 4.6.0. See my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.