Search code examples
gnuplot

GnuPlot plotting time-zero-based data starting with day-zero on the x axis


I have a file of data where the first column is time in seconds. The start of time is 0, and onward from there.

I want to plot the data with an x-axis formatted as days:hours:minutes:seconds. T=0 should map to 00:00:00:00. I can't figure out how to get days to start at 00 instead of 01. I have tried the below. I also tried setting xrange to [-86400:173000], but that maps to day 365, not 0. Shouldn't it be common to plot some time-sampled data, that may span days, starting with T=0?

It seems that GnuPlot needs a different set of time format characters for zero-based time plotting, instead of date-based. Unless it already has it and I have missed it.

data

0 0
3600 10
7200 30
21600 50
160000 100

GnuPlot script

set xdata time
set format x "%02j:%H:%M:%S"
set timefmt "%s"
set xrange [0:173000]
plot "data" using 1:2 with lines

Plot


Solution

  • I can get you part way there. Gnuplot has a separate set of time formats for relative time. Zero-based and handles positive and negative intervals. It's hard to find in the documentation, but here is a section from "help time_specifiers".

       Format       Explanation
       %tH          +/- hours relative to time=0 (does not wrap at 24)
       %tM          +/- minutes relative to time=0
       %tS          +/- seconds associated with previous tH or tM field
    

    Examples of time format:

    The date format specifiers encode a time in seconds as a clock time on a particular day. So hours run only from 0-23, minutes from 0-59, and negative values correspond to dates prior to the epoch (1-Jan-1970). In order to report a time value in seconds as some number of hours/minutes/seconds relative to a time 0, use time formats %tH %tM %tS. To report a value of -3672.50 seconds

       set format x                 # default date format "12/31/69 \n 22:58"
       set format x "%tH:%tM:%tS"   # "-01:01:12"
       set format x "%.2tH hours"   # "-1.02 hours"
       set format x "%tM:%.2tS"     # "-61:12.50"
    

    Using these relative time formats with your sample data I can get as far as:

    $data << EOD
    0 0
    3600 10
    7200 30
    21600 50
    160000 100
    EOD
    
    set xtics time format "%tH:%tM:%tS"
    set title 'set xtics time format "%tH:%tM:%tS"'
    set xrange [0:173000]
    plot $data using 1:2 with lp
    

    enter image description here

    Now the problem is that there is no equivalent relative day format. Call that a bug or at least a missing feature. Let's take a stab at adding days to the format by hand.

    secperday = 3600*24
    days(t) = gprintf("%02g:", int(t)/secperday)
    hours(t) = strftime("%02tH:%tM:%tS", int(t)%secperday)
    
    # Create ten days worth of tic labels
    # Every six hours with no label; once a day with full label
    set xtics 6*3600 format "" 
    do for [i=0:10] {
        T = day * secperday
        set xtics add ( days(T).hours(T) T )
    }
    plot $data using 1:2 with lp
    

    enter image description here