Search code examples
gnuplot

How to plot a arrow on time using the x axis?


I am trying to get an arrow line on the plot using time ie 10:30:00. I had tried various formats of date & time but am getting the following error "naa_8c.plt" line 21: undefined variable: set. The script plots the data OK without the arrow command. How do I define the variable to be used with the arrow command?

Program script

clear

reset

set xdata time

set timefmt '%Y-%m-%d, %H:%M'

set format x "%H:%M"

set datafile separator ","
set key left top
set title "Test Title" font "sans, 14" 
set ylabel "ylable title"
set xlabel "UT Time"
set autoscale y  
set ytics textcolor rgb "blue"
set grid

plot '2018-03-02 naa x ray.csv' u 1:4 w lines lt rgb "red" ,set arrow from "103000",graph 0 to "103000",graph 1 nohead lt 2 lc rgb 'red' lw 2

replot

Data file 2018-03-02 naa x ray.csv

Date,time,Short,NAA-24k

2018-03-02,00:00:29,2.87E-08,-86.4332
2018-03-02,00:01:30,0.000000027,-86.6562
2018-03-02,00:02:29,2.83E-08,-86.5777
2018-03-02,00:03:30,2.55E-08,-86.5437
2018-03-02,00:04:30,0.000000031,-86.6023
2018-03-02,00:05:29,3.02E-08,-86.7367
2018-03-02,00:06:30,3.05E-08,-86.6627
2018-03-02,00:07:29,2.55E-08,-86.5023
2018-03-02,00:08:30,2.73E-08,-86.6553
2018-03-02,00:09:29,2.81E-08,-86.6298
2018-03-02,00:10:30,2.75E-08,-86.4740
2018-03-02,00:11:29,2.74E-08,-86.5313

A lot more data in file

I have tried using various formats of date & or time getting the same error. I have searched the web but have been unable to find a solution that works, they all seem to use the date and not the time.

I am wanting a line on the plot at the time set in the variable, the line is being used as a marker on the plot.


Solution

  • As I understand you want to draw a vertical line from the bottom of the graph to the top of the graph at a given time position on the x-axis.

    In the example below I'm using the syntax without set xdata time. Then you have to use timecolumn(), check help timecolumn and help strptime.

    This is ok if you know your x-value in advance. However, if you want to determine the x-position automatically from your data (e.g. at minimum y-value), you have to add a few lines.

    Script:

    ### set an arrow on time axis
    reset session
    
    $Data <<EOD
    Date,time,Short,NAA-24k
    2018-03-02,00:00:29,2.87E-08,-86.4332
    2018-03-02,00:01:30,0.000000027,-86.6562
    2018-03-02,00:02:29,2.83E-08,-86.5777
    2018-03-02,00:03:30,2.55E-08,-86.5437
    2018-03-02,00:04:30,0.000000031,-86.6023
    2018-03-02,00:05:29,3.02E-08,-86.7367
    2018-03-02,00:06:30,3.05E-08,-86.6627
    2018-03-02,00:07:29,2.55E-08,-86.5023
    2018-03-02,00:08:30,2.73E-08,-86.6553
    2018-03-02,00:09:29,2.81E-08,-86.6298
    2018-03-02,00:10:30,2.75E-08,-86.4740
    2018-03-02,00:11:29,2.74E-08,-86.5313
    EOD
    
    set datafile separator comma
    
    myTimeFmt = "%Y-%m-%d,%H:%M:%S"
    set format x "%H:%M" timedate
    myMarker = strptime(myTimeFmt,"2018-03-02,00:05:29")
    
    set arrow 1 from myMarker, graph 0 to myMarker, graph 1 lw 2 lc "red" nohead
    
    plot $Data u (timecolumn(1,myTimeFmt)):4 w l ti "Data"
    ### end of script
    

    Result:

    enter image description here