I am trying to plot a visualization of the least square method. In the end it should look like this:
Right now I have the data points and I have the fitted curve (a straight line in my case, called f(x)). What I am missing are the residuals (in the link the residuals are the green lines). Meaning: I want to draw lines starting at each data point going vertically to the fitted line.
My data.txt looks like this (shortend)
1, 3
2, 4
3, 6
4, 3
My gnuplot commands are:
f(x) = a*x +b + 1e-9
fit f(x) 'data.txt' u 1:2 via a, b
plot 'data.txt' with points title 'data', f(x)
So far with I only managed to have the residuals as impulses using
plot 'data.txt' with points title 'data', '' using 1:($2 - f($1)) w impulse, f(x)
but like that, the start from y=0 und go up down, they are not between the fitted line f(x) and the data point.
I think you're looking for errorbars
here:
plot 'data.txt' with points title 'data',\
f(x) notitle,\
'data.txt' u ($1):(f($1)):(f($1)):2 w yerrorbars title 'residuals'
There's also the errorlines
style which you could use basically the same:
plot 'data.txt' with points title 'data',\
'data.txt' u ($1):(f($1)):(f($1)):2 w yerrorlines title 'residuals'
but in this case, the line (f(x)
) is plotted with the same style that the residuals are plotted.