5

How can I show the max. and/or the min. value(s) of of a graph in a plot at their appropriate position automatically?

Genschman
  • 177
  • 1
  • 1
  • 11

1 Answers1

8

You can do this "semi-automatically" using the stats command. This command can extract some statistical values from a data set, but requires some reworking:

  1. Extract the minimum and maximum y-values, assuming that your data file has two columns, the x-value in the first, the y-values in the second column

    stats 'file.dat' using 2 nooutput name 'Y_'
    

    This gives you the min/max y-values in the variables Y_min and Y_max, but not the corresponding x-value.

  2. The previous step gives you only get the respective indices, which requires you to run stats again in order to get the x-values:

     stats 'file.dat' using 1 every ::Y_index_min::Y_index_min nooutput
     X_min = STATS_min
     stats 'file.dat' using 1 every ::Y_index_max::Y_index_max nooutput
     X_max = STATS_max
    
  3. Set labels and/or points at the respective coordinates

    set label 1 sprintf("%.2f", Y_min) center at first X_min,Y_min point pt 7 ps 1 offset 0,-1.5
    set label 2 sprintf("%.2f", Y_max) center at first X_max,Y_max point pt 7 ps 1 offset 0,1.5
    ...
    plot ...
    
Christoph
  • 47,569
  • 8
  • 87
  • 187
  • Looks nice. Doesn't work if x series is time data though. "Stats command not available in timedata mode" – gaoithe Oct 08 '15 at 13:55
  • 3
    @gaoithe Yes, I know. For time data you must do something like `stats 'file.dat' using 2 nooutput name 'Y_'; set timefmt '%H:%M:%S'; stats 'file.dat' using (timecolumn(1)) every ::Y_index_min::Y_index_min noutput; ...; set xdata time; plot ...` – Christoph Oct 08 '15 at 14:13
  • yes, thank you. I found your answer over here helpful: http://stackoverflow.com/questions/17977086/plotting-only-the-common-data-in-a-data-series-with-gnuplot My solution more complicated as was using whitespace-field-seperated file. With date/time inside quotes with whitespace inside that string too. Had to convert to .csv. stats command very useful! – gaoithe Oct 08 '15 at 15:34
  • @gaoithe You don't need csv, ``timecolumn` handles white-space separated date/time fields correctly. – Christoph Oct 09 '15 at 05:51
  • gnuplot 5.2: Stats command not available in polar mode – dosmanak Jan 02 '21 at 14:18