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

```reset
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. Great post

5. 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. http://gnuplot.sourceforge.net/demo_canvas_5.2/stats.html