1

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 ?

fae
  • 27
  • 5
  • Maybe try something like https://stackoverflow.com/questions/11092608/gnuplot-plotting-data-from-multiple-input-files-in-a-single-graph? – Marijn May 15 '23 at 14:14
  • Welcome to StackOverflow! This a bit too little information. How many files, what is your fitting function and parameters, etc.? What have you searched and what have you found and what exactly have you tried? Please show your code. Have you checked gnuplot `help fit` or the gnuplot homepage or StackOverlfow for suitable examples? – theozh May 15 '23 at 18:11
  • Actually, do you want to merge several files to one dataset and fit this data as a whole (that's probably what these `cat, awk, join' commands are doing) or do you want to fit several curves with the same function but variable parameters shown in one plot? – theozh May 15 '23 at 18:55
  • @theozh Actually I want to merge several files to one dataset and fit this data (but I am worry becuase of the too many information it will not work). – fae May 16 '23 at 05:34
  • @fae gnuplot is supposed to handle large data. How large is your data MB, GB? What operation system are you working with and which gnuplot version? – theozh May 16 '23 at 06:44
  • @theozh I have right now 5 files with different sizes (from 11 to 58 MB) but in the future more will be added. I am working with Windows and I am using the latest version of Gnuplot. – fae May 16 '23 at 11:12

1 Answers1

0

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'"

theozh
  • 22,244
  • 5
  • 28
  • 72
  • Hello, it's 2 days that I try. I checked if maybe I made an error whyle copying your code, or looking if I choose the right file path (c:\path1\path2\path3\path4\path5\path6.txt), and checked also if the txt.file where in ANSI. But somehow when I let it run it shows me in line where there is "plot for [FILE in FILES] FILE u 1:2 w table" ---> warning: Cannot find or open file "C:path1.path2.path3" ---> warning: Cannot find or open file "path4.path5.path6".......it breaks my file path in 2. I am using geany to code it and then load the file in gnuplot and until now it always worked. – fae May 19 '23 at 14:17
  • @fae ok, I guess Windows will make some difficulties with backslash, single- and double quotes. Try to convert all backslashes into foreslashes. Inc case your path contains spaces put the path into quotes. – theozh May 19 '23 at 14:39
  • it worked, but now I have another problem. I tried it with the column 1:2 it was fine but if I change the column ( I have many columns and if I try for exp. 1:3) in the table, fit and plot it it will give me: Read 0 points "path......." No data to fit. Wich seems very strange. – fae May 22 '23 at 09:24
  • @fae what is your column separator? Maybe comma or something else? Please show an example of a data line. – theozh May 22 '23 at 09:28
  • here: 7.000000000000000 5.750000000000000 4.969999790191650, a very big space between the columns but it's only a blank space at the end, (I choose a lot of decimals for the numbers becuase it makes it better while exporting from excell to txt with the column alligment) – fae May 22 '23 at 09:46
  • @far for printing to the table you need `u 1:3`, but with this, you get a table with **two** columns. Hence, for the fitting and plotting you need `u 1:2`. Maybe this is the "problem"? – theozh May 22 '23 at 10:05
  • sorry I expressed myself wrongly. In my 2 data file there are in each the same amount of columns. Instead to use column 1 and column 2, I want to use column 1 and column 3. So If I change the lines into: FILE u 1:3 w table.....fit f(x) $Data u 1:3......plot $Data u 1:3. Shouldn't I get a graph with the fit and the plot of my columns 1 and 3 ? Or am I missing somethingh ? – fae May 22 '23 at 10:18
  • @fae that's what I was trying to say: you need `FILE u 1:3 w table` but `fit f(x) $Data u 1:2` and `plot $Data u 1:2`. – theozh May 22 '23 at 11:46
  • Thanks very much. I didn't understand it before – fae May 22 '23 at 13:41