Marki
Marki

Reputation: 661

gnuplot: histogram of events: issue with timecolumn()

I would like to see the number of events per timeperiod.

My rows look like this

"2020-11-11 09:15:50",field2,field3

This is what I have tried

binwidth = 3600 # 1h in seconds
bin(t) = (t - (int(t) % binwidth) + binwidth/2)
set datafile separator ","
#set xdata time
set timefmt '"%Y-%m-%d %H:%M:%S"'
set boxwidth binwidth
plot 'Statistics.log' using (bin(timecolumn(1, '"%Y-%m-%d %H:%M:%S"'))):(1) smooth freq with boxes

I'm getting

unknown type in magnitude()

How would I debug errors like these? (How do I dump what gnuplot "sees" for timecolumn() etc.?)

(gnuplot 4.6)

Upvotes: 0

Views: 235

Answers (2)

theozh
theozh

Reputation: 25714

A few minutes too late... while testing... @binzo basically already answered.

The only difference: if your data uses double quotes for the date

"2020-11-11 09:15:50",field2,field3`

and you don't want to change your existing data, you have to specify it in set timefmt. For some strange reason which I cannot explain right now, if you set datafile separator "," it will mess up the graph, but it seems to work without.

Code: (tested with gnuplot 4.6.0)

### timedata in histogram (gnuplot 4.6)
reset

FILE = 'Statistics.log'
myTimeFmt = '"%Y-%m-%d %H:%M:%S"'

# create some test data
myDate = strptime(myTimeFmt, '"2020-11-11 11:11:11"')
myRandomDate(n) = myDate + 3*3600*invnorm(rand(0))
set print FILE
    do for [i=1:500] {
        print sprintf("%s,%g,%g",strftime(myTimeFmt,myRandomDate(0)),rand(0),rand(0))
    }
set print

# set datafile separator ","   # if uncommented this will messup the plot, don't know why
set xdata time
set format x "%Y-%m-%d\n%H:%M"
set timefmt '"%Y-%m-%d %H:%M:%S"'

binwidth = 3600         # 1 h in seconds
bin(t) = (t - (int(t) % binwidth) + binwidth/2)
set boxwidth binwidth
set style fill solid 0.5
set xtics 4*3600     # 4 h in seconds

plot FILE u (bin(timecolumn(1))):(1) smooth freq w boxes notitle
### end of code

Result:

enter image description here

Upvotes: 1

binzo
binzo

Reputation: 1569

At first, The timecolumn() in gnuplot 4.6 is a single-argument function, and only the argument for the column number is allowed. Therefore, the plot command can be rewritten as,

plot "test.dat" using (bin(timecolumn(1))):(1) smooth freq with boxes

Secondly, do not include leading and trailing double quotes in your timefmt formatting.

set timefmt '%Y-%m-%d %H:%M:%S'

For more information about this, please refer to the "help data" section.

...

However, whitespace inside a pair of double quotes is ignored when counting columns, so the following datafile line has three columns:

1.0 "second column" 3.0

Finally, your code can be modified as follows (for gnuplot 4.6)

binwidth = 3600 # 1h in seconds
bin(t) = (t - (int(t) % binwidth) + binwidth/2)
set datafile separator ","
set xdata time
set timefmt '%Y-%m-%d %H:%M:%S'
set boxwidth binwidth
plot 'Statistics.log' using (bin(timecolumn(1))):(1) smooth freq with boxes

Upvotes: 1

Related Questions