Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using Gnuplot, how can the equation of a line be printed in the line title?

I have used Gnuplot to plot my data, along with a linear regression line. Currently, the 'title' of this line, which has its equation calculated by Gnuplot, is just "f(x)". However, I would like the title to be the equation of the regression line, e.g. "y=mx+c".

I can do this manually by reading off 'm' and 'c' from the plotting info output, then re-plot with the new title. I would like this process to be automated, and was wondering if this can be done, and how to go about doing it.

like image 575
JPK Avatar asked Jan 30 '12 20:01

JPK


People also ask

Which of the following command will used to plot a vertical line in gnuplot?

to draw a vertical line The range of t is controlled by the command set trange . In this case the vertical line is draw at x=3.

What is gnuplot command?

Gnuplot is a free, command-driven, interactive, function and data plotting program. Pre-compiled executeables and source code for Gnuplot 4.2. 4 may be downloaded for OS X, Windows, OS2, DOS, and Linux.

Why do we use gnuplot?

gnuplot is a command-driven interactive function plotting program. It can be used to plot functions and data points in both two- and three- dimensional plots in many different formats. It is designed primarily for the visual display of scientific data.


2 Answers

With a data file Data.csv:

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

you can do a linear fitting with:

f(x) = a*x + b

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

You can use what I think is called a macro in gnuplot to set the title in the legend of you identified function f(x) with

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

Now in order to plot the data with the regression function f(x) simply do:

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

You should end up with this plot:

enter image description here

like image 181
Woltan Avatar answered Oct 02 '22 21:10

Woltan


From Correlation coefficient on gnuplot :

Another, perhaps slightly shorter way than Woltan's of doing the same thing may 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)
like image 30
hdl Avatar answered Oct 02 '22 22:10

hdl