Search code examples
gnuplot

Using 'filledcurves' with fitted data


I am fitting some data using 'fit', getting f(x) and plotting it just fine. However, in addition to data points and fitted curve, I wanted to add а confidence interval to the plot, so I would need a "stripe" between f(x)-stddev_y and f(x)+stddev_y.

I could do it writing borders f(x)-stddev_y and f(x)+stddev_y into a table, something like this

set datafile separator ';'
f(x) = a*x + b
fit f(x) '0706-2007.csv' using 7:8 via a,b
stddev_y = sqrt(FIT_WSSR / (FIT_NDF + 1 ))
set table "1.dat"
plot [10:80] f(x)-sqrt(FIT_WSSR / (FIT_NDF + 1 ))
set table "2.dat"
plot [10:80] f(x)+sqrt(FIT_WSSR / (FIT_NDF + 1 ))
unset table
plot [10:80] '0706-2007.csv' using 7:8 not with points pt 7 ps 1,f(x) not with lines,\
"< paste 1.dat 2.dat | awk '{print $1\";\"$2\";\"$5;}' " using 1:2:3 with filledcurves title sprintf("σ = %.1f",sqrt(FIT_WSSR/(FIT_NDF+1)))

but it looks somewhat involved. Is there any more direct approach?


Solution

  • If I understand you correctly you want to fill the area that is covered by the uncertainty of the fit. In your simple case of a linear fit this can indeed be easily done with a constant shift factor. One can make use of the special filename '+' (see the manual on special-filename for details).

    set datafile separator ';'
    f(x) = a*x + b
    fit f(x) '0706-2007.csv' using 7:8 via a,b
    stddev_y = sqrt(FIT_WSSR / (FIT_NDF + 1 ))
    plot '+' using ($1):(f($1)-stddev_y):(f($1)+stddev_y) with filledcurves
    

    If you happen to have a more complicated fit function, you will need to think more carefully what will be the expression for the minimum and maximum value. For that, it might be useful to set fit errorvariables, which will save the fit error of a, b as a_err, b_err