gnuplot - linear fit with x-errors - gnuplot

apologies if this is a very simple question, but I have searched everywhere and can't find a solution. I have a data file with three columns - x:y:xerrors. I want to fit a function f(x) to the data, by entering
f(x)=a*x+b
fit f(x) 'data' using 1:2:3 via a,b
But is this fit assuming that the entries in column 3 are y-errors, when actually they are x-errors?

Change: fit f(x) 'data' using 1:2:3 via a,b
To: fit f(x) 'data' using 1:2:3 with xerrorbars via a,b
This treats the 3rd column as the uncertainty in x and computes an appropriate weight for the WSSR.

Yes the fit assumes that the 3rd column is y-error if you specify
using 1:2:3 else (if you do NOT specify) it assumes all weight equal to 1.
Meanwhile we wait the patch will be included in all the gnuplot distribution,
I can propose a Workaround: # y=ax+b --> x=1./a*(y-b)
f(x)=a*x+b
f2(x)=1./a * (y-b)
fit f2(x) 'data' using 2:1:3 via a,b
plot 'data' with xerrorbar, f(x)
of course this changes the meaning of the uncertainty associated with a and b. See here.

Related

Gnuplot error weighted mean

I have a set of data with y error bars. I can find the mean of the data without error bars using the following:
f(x)=mean_y
fit f(x) "data" via mean_y
However, I want do find a weighted mean, taking into account the error bars. Is something like this possible in gnuplot or do I have to code it?
Thanks in advance.
You can use the using parameter to specify a column for the errors. With three using specifiers, the third one is interpreted as standard deviation s and is used to compute a weight 1/s**2 for the corresponding value:
f(x) = mean_y
fit f(x) "data" using 1:2:3 via mean_y
That assumes, that your data file has three columns, x, z and stdev. If you only have two columns, z and stdev, you must use
fit f(x) "data" using 0:1:2 via mean_y

How to plot a 2 dimensional function using data stored in a file with gnuplot

Is there any way to compute a 2 dimensional function using data from a file in gnuplot. Suppose I have a function f(x,y) which exist and I want to calculate the new values with data stored in file data.dat
i.e something like
plot f(x,y) using 'data.dat'$1:'data.dat'$2
The command plot is used for plotting one variable against another. If you want to plot a third value against two others (and obtain something that looks 3D), you'll need the splot command. In this case, the command would look like
splot 'mydata.txt' using 1:2:(f($1,$2))
The using keyword specifies what you want to plot based on the contents of a file. The 1 and 2 means that the x and y coordinates will just be the first and second column in the file. For the third coordinate we want the f(x,y) function to be used with the values from the first and second column filled in ($1 and $2).
In case we're doing something more complex than just using a column unmodified, we have to use brackets and a $-sign for the variables. So we also could have written
splot 'mydata.txt' using ($1):($2):(f($1,$2))
as the command. See the gnuplot manual for more information.

gnuplot 4.6 errorbars on the fly

Since stats on datafile containing two columns in gnuplot 4.6 provides mean and sd, I was wondering if I can plot errorbars on the fly instead of creating a third column? Thanks!
Assuming a file data.dat with two columns, you could get those error bars with a command like:
stats 'data.dat' nooutput
plot 'data.dat' using 1:2:(STATS_stddev_y) with errorbars
What would this mean, however? All the points have the same standard deviation, which is the value for the whole data set.
I could provide a more complete answer if you describe your data format/data sets in more detail.

xrange limiting in logscale plot in gnuplot

I am trying to fit a plot in gnuplot using logscale. I have 50000 data points.
At first I fit plot in this way.
f(x) = b + m*x
fit f(x) "xyMSD-all-mal-cel-iso-bcm-thermo2.dat" using 1:2 via m,b
I got slope value. Then I tried to get slope value at different range as below.
fit [30000:50000] f(x) "xyMSD-all-mal-cel-iso-bcm-thermo2.dat" using 1:2 via m,b
The above code works fine. In next attempt I tried,
f(x) = b + m*x
fit f(x) "xyMSD-all-mal-cel-iso-bcm-thermo2.dat" using (log($1)):(log($2)) via m,b
Above works fine too. I get the slope value. Then I tried to choose the xrange like below. This is where I have problem. It does not work.
fit [500:5000] f(x) "xyMSD-all-mal-cel-iso-bcm-thermo2.dat" using (log($1)):(log($2)) via m,b
Is there any way to achieve this?
Appreciate any help
The range has to fit the expression, which in your case are log values. So make sure the log values are within range. For example, if your range for ($1):($2) is [500:5000], then the corresponding range for (log($1)):(log($2)) should be something like [2.69:3.69].
Gnuplot first uses the expression on your data. Limiting the range is the second step, so in this case the logarithm of the required data points have to be in the xrange.
AND don't forget: logscale uses the logarithm based on 10 but log(x) or log($1) means logaritm based on 'e' (approx. 2.7183). To be harmonic with the logscale use function log10(x) (or log(x)/log(10)).
PS: I know that the original question had been answered previously, but I haven't got enough prestige to append my useful comment about the log() function as a comment.

custom y scale in plot for

I have a data file, looking like
550 1.436e+00 7.857e-01 5.906e-01 4.994e-01 4.574e-01 4.368e-01 4.260e-01 4.273e-01 4.296e-01 4.406e-01 4.507e-01 4.639e-01 4.821e-01 5.008e-01 5.156e-01 5.378e-01 5.589e-01 5.768e-01 5.970e-01 6.196e-01 6.422e-01 6.642e-01
The first column is for x-axis, the rest ones are for the y-axis, 22 curves totally.
I want to plot the data so that y tics represent cube roots of the values. Actually, I want my cubic curves to become linear, to show, that they're cubic in the normal coordinates (and it is fixed by my task to use these coordinates).
I tried to use the following command:
plot for [i=2:23] datafile using 1:(i ** .333) smooth cspline
It expects column number in place of i.
I know, the following is correct:
plot datafile using 1:($2 ** .333) smooth cspline
giving me the desired plot for my first line. But how do I modify this for plot for?
If you want the column number in place of i, you should use column(i) in the using specification.
plot for [i=2:23] datafile using 1:(column(i) ** .333) smooth cspline

Resources