lalchimista
lalchimista

Reputation: 21

gnuplot fit and rewrite plot inside loop

I have an issue with gnuplot. I do need to fit two data files (say file1 and file 2) that have 51 columns. I do it in a loop like this

do for [j=2:51] {
                 fxj(x) = Dxj*x+ qxj
                 fit [xmin:xmax] fxj(x) '< paste file1 file2' u 1:(((column(j))+(column(j+51)))/2)  via Dxj, qxj
                 print j, '   ', Dxj/2
                }

Everything wonderful. What i do need now is to plot every fxj defined in the loop over the data in order to have the raw data and the fit overlapped. I tried it like this

do for [j=2:51] {
                 fxj(x) = Dxj*x+ qxj
                 fit [xmin:xmax] fxj(x) '< paste file1 file2' u 1:(((column(j))+(column(j+51)))/2)  via Dxj, qxj
                 print j, '   ', Dxj/2
                 plot '< paste file1 file2' u 1:(((column(j))+(column(j+51)))/2) t'', fxj(x) t''
                }

but it doesn't work. Do you have any suggestion to make it work?

Upvotes: 0

Views: 197

Answers (1)

ewcz
ewcz

Reputation: 13087

One approach could be in terms of multiplot as in the simplified example below. The idea is to fix the plot margins so that each consecutive plot within the multiplot environment plots over the same "area". Also, inside the loop, the script makes sure that for all the plots but the first one it unsets the tics etc. so that they are not drawn several times...

set multiplot

set lmargin at screen 0.1
set rmargin at screen 0.9
set bmargin at screen 0.1
set tmargin at screen 0.9

set xr [-2:2]
set yr [-4:4]

do for [j=1:3]{

    if(j>1){
        unset xtics;
        unset ytics;
        unset border;
        unset xlabel;
        unset ylabel;
    }

    set key at screen 0.3,0.9 - j*0.05 reverse
    plot j*x w l t sprintf('plot %d', j);
}

This would produce: enter image description here

Alternatively, you could first run the do loop, assemble the coefficients inside an array and then plot everything at once:

set xr [-2:2]
set yr [-4:4]

array coeffs_a[3]
array coeffs_b[3]

do for [j=1:3]{

    #save the fitted coefficients
    coeffs_a[j] = j
    coeffs_b[j] = j

}

plot for [j=1:3] coeffs_a[j]*x + coeffs_b[j] w l t sprintf('plot %d', j)

Upvotes: 1

Related Questions