Saturday, May 19, 2012

How to pick out the maximum and minimum points when plotting with gnuplot?

Picking out the maximum and minimum points from a data file may be useful when you want to make some explanation to your graph. It is known that GPVAL_Y_MAX (GPVAL_Y_MIN) is the maximum (minimum) value. But what is the corresponding X coordinate?

With Gnuplot 4.6 the both the x and y coordinate of maximum and minimum points can be find out easily. The method is using new command "stats". This command is used for statistic. When it is run, some statistical results will be gotten. If your data file contains two column of data, (STATS_pos_max_y, STATS_max_y) will be the coordinate of the maximum point and (STATS_pos_min_y, STATS_min_y) will be the coordinate of the minimum point.

For example, we have a data file named "data.dat" like this

0.1     0.289010715
0.2     0.050630492
0.3     0.721247895
0.4     0.284271151
0.5     0.505051253
0.6     0.101819025
0.7     0.008466133
0.8     0.36249047
0.9     0.487576233
1.0     0.595090343
1.1     0.865255938
1.2     0.696628854
1.3     0.505899456
1.4     0.338131983
1.5     0.108034045

Run a gnuplot script as follows

set term png
set output "max_min.png"
stats "data.dat" u 1:2 nooutput
set xrange [STATS_min_x:STATS_max_x]
set label 1 "Maximun" at STATS_pos_max_y, STATS_max_y offset 1,-0.5
set label 2 "Minimun" at STATS_pos_min_y, STATS_min_y offset 1,0.5
plot "data.dat" w p pt 3 lc rgb"#ff0000" notitle, \
     STATS_min_y w l lc rgb"#00ffff" notitle, \
     STATS_max_y w l lc rgb"#00ffff" notitle
set output

And you will get a graph like the following one.
Picking out the maximum and minimum points when plotting using gnuplot


  1. Thanks for the useful hint, but I'm using Debian with Gnuplot 4.4...

    And one more thing - just want to thank you for the excellent posts. Seriously. The posts about breaking the axis in gnuplot saved me a lot of time and allowed to prepare a conference proceeding. Thanks.

  2. What can one do when we have multiple data files to be plotted in the same graph and display the max, min values for each file?

  3. ^same question here! Thanks in advance.

  4. What if there are more than two columns and I am interested in, say, the maximum value of the 10th column?

    1. Then you do like this.

  5. IOT Training in Bangalore - Live Online & Classroom
    Iot Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Iot Training in Bangalore

  6. Gaining Python certifications will validate your skills and advance your career.
    python certification

  7. Great post!

    What if you want STATS_pos_max_y to use the absolute values of y? In other words, -8 should be considered larger than 7.

    Also, I am printing STATS_pos_max_y on the plot and I can tell just by looking at the plot that the point is claims is the max, is not the max. How can this be? Is there a sampling frequency or something that could cause it to miss points?

  8. I figured out that STATS_pos_max_y issue. I should have been using STATS_max_y. By switching the two, I was getting the y coordinate, not the y value. Still curious about finding the max of abs(y).

    Presumably I could create a new data set defined by something such as f(y) = abs(y) and then run STATS on the new data set, but that seems inelegant.


Creative Commons License
Except as otherwise noted, the content of this page is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.