Search code examples
gnuplot

Gnuplot. How to plot graf with X time, Y data and find Min and Max on graf


I have following data source with timestamps.

2023-07-19 13:56:37 355
2023-07-19 13:56:41 7
2023-07-19 13:56:43 36
2023-07-19 13:56:52 14
2023-07-19 13:56:54 521
2023-07-19 13:57:09 1642
2023-07-19 13:57:14 512
2023-07-19 13:57:17 171
2023-07-19 13:57:19 407
2023-07-19 13:57:21 527

And I create following commands, but max/min did not appear on graf.

reset
stats "source.txt" using 3 nooutput
set xdata time
set timefmt "%Y-%m-%d %H:%M:%S"
set format x "%d-%b\n%H:%M"
set label 1 sprintf("%.2f", STATS_min) center at first STATS_index_min,STATS_min point pt 7 ps 1 offset 0,-1.5
set label 2 sprintf("%.2f", STATS_max) center at first STATS_index_max,STATS_max point pt 7 ps 1 offset 0,1.5
plot "source.txt" u 1:3 with lines

Could you please help plot graf with max/min labels.


Solution

  • The reason why you don't see your min/max points is because you do stats just on column 3. gnuplot implicitly assumes that the x-values are in pseudocolumn 0 (basically the zero-based line index, check help pseudocolumns). Furthermore, if you do stats when set xdata time you will get an error message "Stats command not available in timedata mode". So, I would use the newer syntax for time data without set xdata time but with timecolumn() (check help timecolumn).

    Now you can use stats with timeformat and the x-positions you will find in STATS_pos_min_y and STATS_pos_max_y.

    Check the following modified script:

    Script:

    ### timedate min/max labels
    reset session
    
    $Data <<EOD
    2023-07-19 13:56:37 355
    2023-07-19 13:56:41 7
    2023-07-19 13:56:43 36
    2023-07-19 13:56:52 14
    2023-07-19 13:56:54 521
    2023-07-19 13:57:09 1642
    2023-07-19 13:57:14 512
    2023-07-19 13:57:17 171
    2023-07-19 13:57:19 407
    2023-07-19 13:57:21 527
    EOD
    
    myFmt = "%Y-%m-%d %H:%M:%S"
    
    stats $Data u (timecolumn(1,myFmt)):3 nooutput
    set format x "%d-%b\n%H:%M" timedate
    set label 1 sprintf("%.2f", STATS_min_y) center at first STATS_pos_min_y ,STATS_min_y point pt 7 ps 1 lc "red" offset 0,1
    set label 2 sprintf("%.2f", STATS_max_y) center at first STATS_pos_max_y ,STATS_max_y point pt 7 ps 1 lc "red" offset 0,1
    set key noautotitle
    
    plot $Data u (timecolumn(1,myFmt)):3 w lines
    ### end of script
    

    Result:

    enter image description here