Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gnuplot plotting against time-difference

Tags:

gnuplot

I am trying to plot some data which has time (date) on the x-axis in the format 2015-12-20. I do not want to plot my data against the absolute date, but against the time difference between that date and another reference "zero" date. The reference date is in same format, eg: 2015-12-15.

Is it possible to have the data file in the format:

%Y-%m-%d Value

%Y-%m-%d Value

but have gnuplot create a plot in which the x-axis is number of days since a reference date?

like image 662
Chah Avatar asked Sep 16 '25 07:09

Chah


2 Answers

This isn't ideal, and there probably is a better way, but this works (I assume that column 1 is your date column and that column 2 is your y-axis value):

plot "data.txt" u ((strptime("%Y-%m-%d",strcol(1))-strptime("%Y-%m-%d","2015-12-10"))/86400.0):2

The strptime function turns a time string to an internal time representation (the number of seconds since some reference date) and the strcol function reads the string from column 1. We take the difference from the "zero" date (here we use December 10th). This will give the difference in seconds, so we divide by the number of seconds in a day, 86400 (we use 86400.0 so that it doesn't truncate to integers in the division). This will make the x-axis the number of days since the reference date.

like image 197
Matthew Avatar answered Sep 17 '25 21:09

Matthew


Matthew's suggestion is the right direction. The following script differentiates slightly:

  • definition of a separate function to make the actual plot command shorter and clearer.
  • the manually inserted fixed starting date is automatically replaced by the first date of the dataset. Which is probably also OP's intention.

The following example requires gnuplot>=5.0.0 because of the use of datablocks. But it can certainly be adapted to work with gnuplot 4.x versions.

Script: (works for gnuplot>=5.0.0, Jan. 2015)

### plot time difference to first date entry
reset session

$Data <<EOD
2015-12-01    1.1
2015-12-03    3.2
2015-12-05    2.3
2015-12-10    5.4
2015-12-13    9.5
2015-12-17    4.6
2015-12-19    7.7
2015-12-24    6.8
EOD

dt(col) = (t=strptime("%Y-%m-%d",strcol(col)), $0==0?(t0=t,0):t-t0)/86400.
set xlabel "Number of days"

plot $Data u (dt(1)):2 w lp pt 7 lc rgb "red" ti "Data"
### end of script

Result:

enter image description here

like image 31
theozh Avatar answered Sep 17 '25 20:09

theozh