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?
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.
Matthew's suggestion is the right direction. The following script differentiates slightly:
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With