Search code examples
gnuplot

mixing date and time on gnuplot xaxis


I'd like to plot hours of the day on the x axis, but print the date when the day rolls over from one day to the next. So the x axis might look like this:

11/02 04:00 08:00 12:00 16:00 20:00 11/03 04:00 08:00 ...

Is there a sane way to do this in gnuplot?

FWIW, my file currently looks something like this:

set xdata time
set timefmt "%Y-%m-%d|%H:%M:%S"
plot '-' using 1:2 with lines linewidth 1 linecolor rgb "#FF0000"
2013-11-02|00:00:48 123.0
2013-11-02|00:00:55 124.0
2013-11-02|00:01:06 121.0
2013-11-02:00:01:17 123.0
...
2013-11-04|23:59:41 241.0
2013-11-04|23:59:52 241.0

Solution

  • That is a tough one, made me think a bit. Here is how you can do it:

    You need to set the format of the x-axis to %H:%M and then replace the 00:00 with dates, similarly like done in this answer.

    The main work is then to extract the timestamps for the mid nights. I use the stats command for this (needs at least version 4.6.0), but because it doesn't support time data you must fiddle around a bit with strptime and similar:

    fmt = "%Y-%m-%d|%H:%M:%S"
    stats 'file.txt' using (strptime(fmt, stringcolumn(1))) nooutput
    t = int(STATS_min)
    t_start = t - tm_hour(t)*60*60 - tm_min(t)*60 - tm_sec(t)
    num_days = 2 + (int(STATS_max) - t)/(24*60*60)
    
    set xdata time
    set timefmt fmt
    set xtics 4*60*60
    set for [i=1:num_days] xtics add (strftime('%m/%d', t_start+(i-1)*24*60*60) t_start+(i-1)*24*60*60)
    set format x '%H:%M'
    plot 'file.txt' using 1:2 with lines
    

    I increment num_days by 2 to account for the possible automatic extension of the x-range to the next tics.

    The result with your data is (with 4.6.4):

    enter image description here