When using Gnuplot, how can the line equation be printed in the line header?

I used Gnuplot to build my data along with a linear regression line. Currently, the “name” of this line, which has its own equation calculated by Gnuplot, is simply “f (x)”. However, I would like the name to be a regression line equation, for example. "Y = x + s."

I can do this manually by reading "m" and "c" from the output of the graph information, and then redoing it with a new heading. I would like this process to be automated, and I was wondering if this can be done and how to do it.

+8
regression gnuplot
source share
2 answers

With data file Data.csv :

 0 0.00000 1 1.00000 2 1.41421 3 1.73205 4 2.00000 5 2.23607 

You can perform linear editing with:

 f(x) = a*x + b fit f(x) 'Data.csv' u 1:2 via a, b 

You can use what I call a macro in gnuplot to set the title in the legend about your identified f(x) function with

 title_f(a,b) = sprintf('f(x) = %.2fx + %.2f', a, b) 

Now, to build the data with the regression function f(x) , simply do:

 plot "Data.csv" u 1:2 wl, f(x) t title_f(a,b) 

You must finish this plot:

enter image description here

+21
source share

From Correlation coefficient on gnuplot :

Another, possibly a bit shorter way, than Voltan, having done the same, could be:

 # This command will analyze your data and set STATS_* variables. See help stats stats Data.csv f(x) = STATS_slope * x + STATS_intercept plot f(x) title sprintf("y=%.2fx+%.2f", STATS_slope, STATS_intercept) 
+1
source share

All Articles