0

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?

2
  • Welcome to SO. Can you add what exactly is not working with your code to the question? This makes it a lot easier to both find your question and answer it. Commented Dec 11, 2017 at 13:22
  • stackoverflow.com/a/46959036/2604213 Commented Dec 11, 2017 at 13:22

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

my favourite: the legend for three identical purple lines.
@mikuszefski ah, indeed, what a shame! :) well, I just wanted to show how to position the legend in the overlapping-plot scenario...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.