Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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
like image 730
user2846740 Avatar asked Oct 04 '13 14:10

user2846740


People also ask

What is offset in gnuplot?

Offsets provide a mechanism to put a boundary around the data inside of an autoscaled graph. Syntax: set offsets <left>, <right>, <top>, <bottom> unset offsets show offsets. Each offset may be a constant or an expression. Each defaults to 0.

How use gnuplot to plot data from a file?

To plot functions simply type: plot [function] at the gnuplot> prompt. Discrete data contained in a file can be displayed by specifying the name of the data file (enclosed in quotes) on the plot or splot command line. Data files should have the data arranged in columns of numbers.

Does gnuplot support multiple Y axes on a single plot?

5.9 Does gnuplot support multiple y-axes on a single plot? Yes. 2D plots can have separate x axes at the bottom (x1) and top (x2), and separate y axes at the left (y1) and right (y2).

What is gnuplot Splot?

splot is the command for drawing 3-d plots (well, actually projections on a 2-d surface, but you knew that). It can create a plot from functions or a data file in a manner very similar to the plot command. See plot (p. ) for features common to the plot (p. ) command; only differences are discussed in detail here.


1 Answers

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

like image 197
Christoph Avatar answered Nov 03 '22 13:11

Christoph