How to add offset to time-based data when plotting in gnuplot

gnuplot

I want to add an offset to the time-based data from a file which I want to plot using gnuplot. I can plot the data just fine, but when I try to add a time-based offset the graph is empty.

The purpose is to plot multiple bars next to each other, like described here, but with time-based data: Two plots "with boxes" next to each other with gnuplot

Data file:

00:00 7719
01:00 20957
02:00 15989
03:00 9711
04:00 1782
05:00 871
06:00 4820
07:00 860
08:00 873
09:00 848
10:00 879
11:00 726
12:00 944
13:00 924
14:00 996
15:00 806
16:00 848
17:00 967
18:00 2277
19:00 2668
20:00 32183
21:00 14414
22:00 20426
23:00 16140

I am trying to plot the data using this code:

set xdata time
set timefmt "%H:%S"
set format x "%H"
set style fill solid 0.6 border -1
set boxwidth 0.3 relative
plot ["00:00":"23:30"] 'data.dat' using ($1-0.3):2 with boxes, \
  'data.dat' using ($1+0.3):2 with boxes

This is only a test – the real data file has additional data columns, and I am trying to place the boxes next to each other using offset, however I'm having no luck with time-based data and offsets.

Without offset the code is fine:

set xdata time
set timefmt "%H:%S"
set format x "%H"
set style fill solid 0.6 border -1
set boxwidth 0.3 relative
plot ["00:00":"23:30"] 'data.dat' using 1:2 with boxes

Best Answer

For computations with timedata you must use the timecolumn() function, which parses a time string from a column according to the set timefmt settings. The result is a time stamp in seconds. So, for the offset you must use the according time in seconds:

set xdata time
set timefmt "%H:%S"
set format x "%H"
set style fill solid 0.6 border -1
set boxwidth 0.3 relative
set xrange["00:00":"23:30"]
set style data boxes
plot 'data.dat' using 1:2, \
     '' using (timecolumn(1)+60*20):($2*0.5), \
     '' using (timecolumn(1)+60*40):($2*0.7)

This gives:

enter image description here

As another variant you can use the histogram style, and format the xtic labels using strftime and timecolumn:

set timefmt "%H:%S"
set style fill solid 0.6 border -1
set style data histogram
set style histogram clustered gap 1
plot 'data.dat' using 2:xtic(strftime('%H', timecolumn(1))), \
     '' using ($2*0.5), \
     '' using ($2*0.7)

That gives:

enter image description here

You get no separation between two data blocks with set style histogram clustered gap 0.

In order to control the tic labelling, you may use something like

plot 'data.dat' using 2:xtic((int($0) % 2 == 0) ? strftime('%H', timecolumn(1)) : '')

which prints only the label of every second entry in the data file ($0 refers to the row number).

Related Topic