Search code examples
gnuplot

Using multiple file (txt) for fitting and plotting


I have multiple files very similar to each other, that I want to plot and fit in one diagramm. To be more precise: multiple curves with the same columns x:y in every file and to use only one fit for all the curves.

I tried for the fit to use "< cat" , "< awk" (this one I fully didn't understand it), "< type" but they are from quite old response and I tried them and they didn't work. For he plot instead I used "< join" but it also didn't work. Don't they work with the newest version of Gnuplot maybe ?


Solution

  • Here is a platform independent solution (gnuplot only). You simply plot your files into a table (datablock) and do a fit on it. I guess, there will also be another solution under Windows using "< type ...". I'm not sure which of the solutions would be better for large data.

    Data:

    SO76254599_1.dat

    -4  15.0
    -3   8.5
    -2   4.5
    -1   1.3
    

    SO76254599_2.dat

     0   0
     1   0.9
     2   2.2
     3   8.7
    

    SO76254599_3.dat

     4  15
     5  24
     6  37
     7  50
    

    Script:

    ### concatenate datafiles and do a fit
    reset session
    
    f(x) = a*x**2 + b*x + c
    
    FILES = "SO76254599_1.dat SO76254599_2.dat SO76254599_3.dat"
    
    set table $Data
        plot for [FILE in FILES] FILE u 1:2 w table
    unset table
    
    fit f(x) $Data u 1:2 via a,b,c
    set key top left
    
    plot $Data u 1:2 w p pt 7 lc "blue", \
         f(x) w l lc "red"
    ### enf of script
    

    Result:

    Final set of parameters            Asymptotic Standard Error
    =======================            ==========================
    a               = 1.02475          +/- 0.02409      (2.351%)
    b               = -0.0186563       +/- 0.1031       (552.8%)
    c               = -0.564311        +/- 0.3619       (64.12%)
    

    I guess the errors of b and c are that large because the values are close to zero.

    enter image description here

    Addition:

    If you have problems with subdirectories or paths under Windows, try to replace backslashes with foreslashes. And if your pathname contains spaces, put the pathname into quotes. For example:

    FILES = "'Test/With Space/SO76254599_1.dat' 'Test/With Space/SO76254599_2.dat' 'Test/With Space/SO76254599_3.dat'"