Gnuplot: Use fit in log scale

Rafael Castro picture Rafael Castro · Apr 5, 2014 · Viewed 7.3k times · Source

I need to make a linear approximation. However it needs to be in a log scale.

Here is my gnuplot script:

f(x)= a*x+b
fit f(x) "d0.dat" via a,b
set logscale x
set logscale y
plot "d0.dat" with points lt rgb "#ff0000" title "Points", \
f(x) with lines lt rgb "#ff00ff" title "Approximation"

enter image description here

Clearly the approximation is wrong. Can anyone help me to fix it. I didn't find any thing in google.

Answer

andyras picture andyras · Apr 5, 2014

Gnuplot is correctly fitting your data to the function you provided--a straight line.

The problem is that using a log scale for the y axis does not scale the data--just how the data are plotted.

Try fitting it to a power law:

f(x)= a*x**b
fit f(x) "d0.dat" via a,b
set logscale x
set logscale y
plot "d0.dat" with points lt rgb "#ff0000" title "Points", \
f(x) with lines lt rgb "#ff00ff" title "Approximation"