Gnuplot seems to fail in linear fit - gnuplot

I have a problem with gnuplot.In my data there is a very obvious linear relation in a specific range. So I want to do a linear fit in that range. But gnuplot throws something like an orthogonal (to as it should be) out.
I created the following fitting-file:
set fit errorvariables
f(x)=a*x+b
fit f(x) 'Data1f.txt' using 1:2:3:4 xyerrors via a,b
g(x)=c*x+d
fit g(x) 'Data2f.txt' using 1:2:3:4 xyerrors via c,d
set xlabel 'U_G [V]'
set ylabel '{/Symbol=\326}(U_{Ph}-U_0) [{/Symbol=\326}U]'
set xrange [0:0.9]
set yrange [0:4.5]
set grid
plot 'Data1.txt' using 1:2:3:4 w xyerrorbars title 'Measurement 1', f(x) title 'f(x)', 'Data2.txt' using 1:2:3:4 w xyerrorbars title 'Masurement 2', g(x) title 'g(x)'
And it results in the following plot:
I don't know why it is not fitting the obviously linear part of the Data ('Data1f' and 'Data2f' contain exact the same values like 'Data1' and 'Data2' just with some data-points left out. (i tried that when the range comands 'fit [number:number] f(x)......' showed the same result.).
Even LibreOffice Calc was able to give me a fitting curve that resulted in the expected one (but LibreOffice Calc can't handle errors - only the data-points itself).

Because you specified the range after fitting, gnuplot fits the linear function to the full data set, which you said is non-linear. Therefore the result is obviously bad. If you change the order of your commands, fit will only use in-range data and you should get a reasonable fit:
set xrange [0:0.9]
set yrange [0:4.5]
fit f(x) 'Data1f.txt' using 1:2:3:4 xyerrors via a,b
fit g(x) 'Data2f.txt' using 1:2:3:4 xyerrors via c,d

You initialize a,b,c, and d with some value nearby that you are expecting. Then try it, it will work. Initially, the value may be some random garbage number and I could fix the same problem by Initializing the constants with some expected nearby values

Related

Spacing between points in graph in gnuplot

As can be seen in the picture attached, the points become too cluttered after a certain point. How do I increase the spacing between points in that region?
Code:
set term jpeg size 1800,900
set output "plot.jpeg"
splot "3d_ME2_31.out" us 1:2:3 lc -1
set grid
set output
I have tried the "every" command but it increasing the spacing in the beginning region too where the point spacing is already high.
Any advice is appreciated. Thanks !
Edit: Data file ( the data to axes mapping is 1:2:3 )
0.94652E+03 0.46588E+02 0.82952E-01
0.94871E+03 0.61601E+02 0.16795E+00
0.95102E+03 0.74087E+02 0.29030E+00
0.95587E+03 0.94012E+02 0.65911E+00
0.95850E+03 0.10204E+03 0.91894E+00
0.96148E+03 0.10885E+03 0.12599E+01
0.96515E+03 0.11542E+03 0.17353E+01
0.96943E+03 0.12040E+03 0.23543E+01
0.97481E+03 0.12578E+03 0.32153E+01
0.98134E+03 0.13077E+03 0.43582E+01
0.98901E+03 0.13507E+03 0.58196E+01
0.99795E+03 0.13948E+03 0.76562E+01
0.10082E+04 0.14377E+03 0.99270E+01
0.10199E+04 0.14800E+03 0.12677E+02
0.10282E+04 0.14775E+03 0.14762E+02
0.10335E+04 0.14413E+03 0.16176E+02
0.10403E+04 0.14050E+03 0.18096E+02
0.10488E+04 0.13621E+03 0.20616E+02
0.10588E+04 0.13205E+03 0.23729E+02
0.10710E+04 0.12798E+03 0.27711E+02
0.10843E+04 0.12370E+03 0.32248E+02
0.10995E+04 0.11973E+03 0.37684E+02
0.11163E+04 0.11572E+03 0.43955E+02
0.11324E+04 0.11203E+03 0.50189E+02
0.11474E+04 0.10875E+03 0.56245E+02
0.11626E+04 0.10553E+03 0.62585E+02
0.11781E+04 0.10217E+03 0.69305E+02
0.11938E+04 0.99038E+02 0.76322E+02
0.12094E+04 0.96412E+02 0.83573E+02
0.12252E+04 0.94002E+02 0.91126E+02
0.12411E+04 0.91794E+02 0.98981E+02
0.12571E+04 0.89765E+02 0.10714E+03
0.12732E+04 0.87918E+02 0.11559E+03
0.12894E+04 0.86536E+02 0.12432E+03
0.13054E+04 0.84701E+02 0.13318E+03
0.13213E+04 0.82945E+02 0.14227E+03
0.13373E+04 0.81284E+02 0.15161E+03
0.13533E+04 0.79678E+02 0.16122E+03
0.13694E+04 0.78071E+02 0.17111E+03
0.13855E+04 0.76542E+02 0.18127E+03
0.14008E+04 0.74960E+02 0.19118E+03
0.14159E+04 0.73330E+02 0.20115E+03
0.14309E+04 0.71898E+02 0.21132E+03
0.14458E+04 0.70357E+02 0.22162E+03
0.14607E+04 0.68300E+02 0.23218E+03
0.14758E+04 0.66454E+02 0.24304E+03
0.14907E+04 0.64530E+02 0.25400E+03
0.15055E+04 0.62676E+02 0.26517E+03
0.15204E+04 0.60999E+02 0.27663E+03
0.15354E+04 0.59429E+02 0.28833E+03
0.15500E+04 0.57764E+02 0.30002E+03
0.15641E+04 0.56109E+02 0.31144E+03
0.15758E+04 0.53850E+02 0.32117E+03
0.15882E+04 0.51118E+02 0.33162E+03
0.16010E+04 0.48445E+02 0.34255E+03
0.16127E+04 0.45813E+02 0.35263E+03
0.16249E+04 0.43266E+02 0.36339E+03
0.16373E+04 0.40811E+02 0.37449E+03
0.16498E+04 0.38485E+02 0.38581E+03
0.16620E+04 0.36479E+02 0.39707E+03
0.16742E+04 0.34549E+02 0.40846E+03
0.16860E+04 0.32632E+02 0.41963E+03
It would be complicated to limit the point reduction to a certain region, but you can do it globally using the pointinterval property of the style with linespoints. If you want to hide the line you can set the linetype to "nodraw": with linespoints lt nodraw pi 5. But that would get you back to the same result as using every to filter the points. The nice thing about using pointinterval instead is that the line goes through all the points even though not all of them are shown. So a jog in the line can highlight an outlier that might otherwise be hidden.
#Plot every 5th point
splot 'DATA' with linespoints pointinterval 5
I would say whether your data points look cluttered or cramped does not only depend on your data but also on your viewpoint.
If you have so many data points why don't you simply plot it with lines? Or as Ethan suggested with linespoints and a certain pointinterval or pointnumber (check help lp).
Script:
set multiplot layout 2,1
set view 60,30
splot $Data u 1:2:3 w p pt 1
set view 71,333
splot $Data u 1:2:3 w p pt 1
unset multiplot
Result: (same datapoints just different viewing angles)

Putting a value from an input data file into 'Set Label'

I'm plotting an animated surface in gnuplot and want to read in an average or sum of the mapped z values and include this in a label to be printed in the plot, so that I get a running total updated as the GIF progresses. It's probably straightforward, but I'm a "gnu"bie, so to speak, and find this system pretty confusing!
I've tried putting the running sum and average numbers in additional columns ...
splot 'output3.dat' index i:i using 1:2:(column(3), TD1 = strcol(4), TD2 = strcol(5)) with pm3d
but this doesn't plot, and the string variables TD1, TD2 don't seem to exist outside the splot command.
The command you show would indeed set variables TD1 and TD2 globally if you change the order of clauses in the serial evaluation expression (the comma-separated sub-expressions):
splot 'output3.dat' index i:i using 1:2:(TD1 = strcol(4), TD2 = strcol(5), column(3)) with pm3d
However, if the idea is to create a label using set label that will appear as part of the resulting graph, this won't work. The set label command would have to be executed before the splot command, so TD1 and TD2 will not have the correct values yet.
There is an alternative that might serve you better. Instead of trying to put this dynamically evaluate information in a label, put it in the plot title. Unlike a label, the plot title is evaluated after the corresponding plot is generated, so any variables set or updated by that plot will be current. [caveat: this is true for current gnuplot (version 5.4) but was not always true. If you have an older gnuplot version the title is evaluated before the plot rather than after].
Since current gnuplot also allows you to place the individual plot titles somewhere other than in the key proper, you have the same freedom that you would with a label to position the text anywhere on the output page. For example, if you want to sum the values in column 3 of the data file and print the total as part of a title above the resulting plot:
SUM = 0
splot 'foo.dat' using 1:2:(SUM = SUM+column(3), column(3)) with linespoints title 'foo.dat', \
keyentry title = sprintf("Points sum to %g", SUM) at screen 0.5 0.9
I used a separate keyentry clause because this allows to omit the sample line segment that would otherwise be generated, but it would also be possible to make this the title of the plot itself if you want that sample line.

plot z value in each data point pm3d map

My question is about my code in gnuplot. I want to plot data with pm3d map and Matrix and want to plot the z value in each datapoint as well. I tried this:
set pm3d;set pm3d interpolate 0,0;set pm3d map;
set Palette rgb 33,13,10;splot 'filepath' Matrix
It is all working fine but I cannot plot the z values so I tried this one
splot 'filepath' Matrix using 1:2:(sprintf(%g,$3)) with labels
but this is also not working. Can anybody help me?
as #Bodo, wrote... "not working" is not enough and not helpful to others.
With your code, you must have gotten some error messages, provide them as well.
Several mistakes in your script:
set palette instead of set Palette
splot 'filepath' matrix instead of splot 'filepath' Matrix
sprintf("%g",$3) instead of sprintf(%g,$3)
To your actual problem:
splot requires 3D coordinates + label text, i.e. in your case x:y:z:z or 1:2:3:3
So, the following code is probably doing what you intended to do.
set pm3d
set pm3d interpolate 0,0
set pm3d map
set palette rgb 33,13,10
splot 'filepath' matrix u 1:2:3:(sprintf("%g",$3)) with labels

Plot error_bars with lines in gnuplot

I have a file with the delivery rate of a simulation and its confidence interval. I need to plot an error bar on my chart. Need to be the line graph and also the error bar on a single chart
Sample data:
V D IC
10 99.2373 0.000200729
30 97.2515 0.00649952
60 94.6761 0.00950475
I would like it to look like the example below:
Here is my code:
set nokey
set grid
set key right inside
set xlabel 'Velocidade em Km/h'
set ylabel 'Taxa de Entrega'
set autoscale
set yr[0:100]
set style data lines
plot 'taxa_entrega-AODV-50-250.txt' using 1:2:($2-$3):($2+$3) with yerrorbars
if you are plotting errorbars you typically plot data with points but not necessarily with connected lines or linespoints. Instead you add a line described by a model or fit.
However, from the image you are referring to I assume you want to have the points nevertheless connected. So, simply add ,\ the same data '' again as lines.
plot 'taxa_entrega-AODV-50-250.txt' using 1:2:($2-$3):($2+$3) with yerrorbars,\
'' using 1:2 with lines
Addition: See below what you get with the full code. (I commented out the set yrange[0:100] because otherwise one wouldn't see too much with the data you provided. Furthermore, your errobars are in the range of 0.0002 to 0.009. Relative to the values 94.6 to 99.2, these will not be bars but more points.
reset session
set nokey
set grid
set key right inside
set xlabel 'Velocidade em Km/h'
set ylabel 'Taxa de Entrega'
set autoscale
# set yrange[0:100]
set style data lines
plot 'taxa_entrega-AODV-50-250.txt' using 1:2:($2-$3):($2+$3) with yerrorbars,\
'' using 1:2 with lines

Splot with pattern-filled closed curves and colorbar in Gnuplot

I've got two datafiles that I need to plot, First datafile "surface.dat" is an Nx3 matrix which contains the x y z data.
Am splotting with pm3d and set viewmap to get the 2D projection map of my current surface where the z data defines the range of the colorbar.
The second data file "closed_curve.dat" lies on the x-y plane with no z components. Its very easy to plot the surface and curve in the same graph by using
set view map
set cbrange...
set xrange...
set yrange...
splot "surface.dat" u 2:1:3 title "" w pm3d ,"closed_curve.dat" u 1:2:(0) title ""
Since its a closed curve however, I want to fill it with one of the gnuplot patterns but I can't find anything that works.
A closed curve in the form of a rectangle for example can just be created as an object without the need of a data file and filled in so that's not a problem e.g
set object 1 rectangle from 0,0 to 0.4,0.8 front fc lt 1 fs pattern 2 lw 2
Thanks in advance....
I think your best option is using an external tool to create a polygon object based on your data file:
set macros
polystr = system('awk -v "ORS= " "{printf \"%s %f,%f \",s,\$1,\$2; s = \"to\"}" closed_curve.dat')
set object 1 polygon fillstyle pattern 5 from #polystr
splot "surface.dat" using 2:1:3 with pm3d
I'm not sure if there is any limit on the allowed line length which you may hit with polystr. Maybe you'll need to write the command to a temporary file and the load this.

Resources