Question

My question is pretty basic. I am plotting several functions at once using gnuplot, and I want to print out (in either a file or on the graph itself) the maximum y-values of every function. Any idea how I could do that?

I looked into STATS and GPVAL_DATA_Y_MAX but I can't really figure out how to make them work with several functions at the same time.

Without going into too much details, let's suppose that my file looks like that :

plot 'file1.dat' us 1:2 title "file1" w lines,\
     'file2.dat' us 1:2 title "file2" w lines,\
     'file3.dat' us 1:2 title "file3" w lines
Was it helpful?

Solution

You can use the name parameter of the stats option to save the maximum of every file in a different set of variables:

stats 'file1.dat' using 2 nooutput name 'file1'
stats 'file2.dat' using 2 nooutput name 'file2'
stats 'file3.dat' using 2 nooutput name 'file3'

Now you can either print the values to an external file

set print 'max.dat'
print file1_max
print file2_max
print file3_max

If you want to place a respective label near the maximum in your graph, you must also know the corresponding x-value where the data has its maximum. This data is not readily available from the first stats command, only its index in the data file. So you need an additional call to stats in order to get the x-value where the maximum y-value was:

stats 'file1.dat' using 1 every ::file1_index_max::file1_index_max name 'file1_x'
...

And then you can use

set label center at first file1_x_max,first file1_max sprintf('y = %.2f', file1_max) offset char 0,1

Unfortunately, most of the commands cannot be iterated properly with changing variable names.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top