Search code examples
gnuplot

gnuplot - Read Double Quoted datetime stamp


I am trying learn gnuplot and I want to to plot some comma separated data.

  • x axis datetime stamp - enclosed in double quotes - containing a comma separator,
  • y axis integer value data.
"1/31/22, 4:36",0
"1/31/22, 16:30",1
"1/31/22, 16:39",2
"2/1/22, 16:44",3
"2/1/22, 9:25",4
"2/7/22, 13:59",5
"2/7/22, 8:57",6
"2/8/22, 11:03",7
"2/18/22, 15:50",8
"2/21/22, 11:11",9
"2/24/22, 9:11",10

How do I read this data and plot in gnuplot?
I tried the following but get error about "warning: Skipping data file with no valid points"

set xdata time
set timefmt "\"%m/%d/%Y, %H:%M\""
set format x “%d/%m/%Y”
set datafile separator ","
plot "file.txt" using 1:2 with linespoints

I assume my timefmt is incorrect. What is the correct syntax?

Also can gnuplot process datetime stamps in the following format with AM/PM?

"1/31/22, 4:36 AM",0
"1/31/22, 4:30 PM",1

Solution

  • Check help time_specifiers. Your year it 2 digits %y not 4 digits %Y. The double quotes will make your date/time the first column. No need for extra double quotes in the format.

    With the syntax set xdata time and set timefmt "..." you have only one format in the plot command. If you are using the syntax timecolumn() (check help timecolumn) you can use different input time formats in your plot command.

    Note: the format %p as input format has been introduced in gnuplot 5.4.0 For older versions you have to use a workaround, see e.g. gnuplot: how to convert 12h time format into 24h time format?

    Code:

    ### plot timedata in double quotes and AM/PM format
    reset session
    
    $Data1 <<EOD
    "1/31/22, 4:36",0
    "1/31/22, 16:30",1
    "1/31/22, 16:39",2
    "2/1/22, 16:44",3
    "2/1/22, 9:25",4
    "2/7/22, 13:59",5
    "2/7/22, 8:57",6
    "2/8/22, 11:03",7
    "2/18/22, 15:50",8
    "2/21/22, 11:11",9
    "2/24/22, 9:11",10
    EOD
    
    $Data2 <<EOD
    "1/31/22, 4:36 AM",0
    "1/31/22, 4:30 PM",1
    "1/31/22, 4:39 PM",2
    "2/1/22,  4:44 PM",3
    "2/1/22, 9:25 AM",4
    "2/7/22, 1:59 PM",5
    "2/7/22, 8:57 AM",6
    "2/8/22, 11:03 AM",7
    "2/18/22, 3:50 PM",8
    "2/21/22, 11:11 AM",9
    "2/24/22, 9:11 AM",10
    EOD
    
    set datafile separator comma
    
    myTimeFmt1 = "%m/%d/%y, %H:%M"
    myTimeFmt2 = "%m/%d/%y, %H:%M %p"
    set format x "%d/%m/%Y" time
    
    plot $Data1 u (timecolumn(1,myTimeFmt1)):2 w lp pt 7 ps 2 lc "grey" lw 4 ti "Data1", \
         $Data2 u (timecolumn(1,myTimeFmt2)):2 w lp pt 7 ps 1 lc "red"  lw 1 ti "Data2"
    ### end of code
    

    Result:

    enter image description here