why gnuplot does not plot after fit? - plot

I am trying to fit a linear curve, and then plot the graph with the fitted line as well. However, the following does not work, as gnuplot makes a 0 kbyte .png output.
set t push
set term png
fit f(x) "NET_OUTPUT.dat" u 1:2 via a,b
plot "NET_OUTPUT.dat" using 1:2 with points, f(x)
unset output
set term pop
If the fit line is not included, I can plot without problem.
I suspect that the problem is that the fit.log file is written, and the gnuplot does not finish the writing of the output file.
Does somebody know what is the problem?
Thanks a lot!

For gnuplot when plotting to a png I typically create a script like:
#!/usr/bin/gnuplot
set term png
set output "out.png"
f(x) = a*x + b
fit f(x) "data.dat" u 1:2 via a,b
plot "data.dat" u 1:2, f(x)
and call it with ./plot.gnu or I create a script like:
#!/usr/bin/gnuplot
set term png
f(x) = a*x + b
fit f(x) "data.dat" u 1:2 via a,b
plot "data.dat" u 1:2, f(x)
and call it with ./plot.gnu > out.png.
The first way asks gnuplot to write a png to the specified output file. The second ask gnuplot to generate the png but doesn't set a location (so it gets dumped to standard output), in this case I redirect the standard output (in bash with >) to my png file.
You can also pass the script into gnuplot with gnuplot plot.gnu or gnuplot plot.gnu > out.png on the command line in which case the lines beginning with # are ignored as comments.

Related

unexpected line in a plot of function defined by parts - gnuplot

I'm trying to plot a piece wise defined function give by,
equation
Implemented in gnuplot, like this,
h(x)=0
g(x)=(4/3)*(1-x**3)
plot h(x)*(x<0) + g(x)*(x>0)*(x<1) + h(x)*(x>1)
The problem is that the line that goes from h(x) to g(x) is not a vertical line, is a inclined one. I really need a vertical one.
How to fix it?
plot
You have to increase the sampling rate, for example by
set samples 1000
Also note that you might have to change the definition of the function g(x) to
g(x)=(4./3.)*(1-x**3)
because you otherwise evaluate (4/3), which is equal to 1 rather than 1.333...
You mustn't have (mathematically speaking) a connecting line between the noncontinous parts, because that implies that there are actual function values there.
You should do this instead:
plot sample [:0] h(x) lc 1 title "f(x)", \
[0:1] g(x) lc 1 notitle, \
[1:] h(x) lc 1 notitle

GNU plot - count the number of peaks

I have a very huge text file with 11 columns. As I can't post the whole data, I have uploded the text file to a public repo and is found in this link: http://s000.tinyupload.com/?file_id=59483318155908771897
Is there any way to COUNT the number of peaks using GNU plot in Linux? From the above text file, I am plotting the 1st and 7th column as x and y columns where the peaks are variations of the 7th column and that's what I am interested in. For example, to count the number of peaks of frequency as in the following image as 10.
Here a simple plotting script i am using.
set key right top
set xrange [:10]
#show timestamp
set xlabel "time in sec"
set ylabel "Freq"
set title "Testing"
plot "data/freq.csv" using 1:7 title "Freq", \
Thanks for any help.
Gnuplot is for plotting and minor arithmetic, finding peaks in a signal is a signal processing task and you need something like GNU Octave to do a reasonable job. If you load freq.csv file and run findpeaks() on it with a plausible value for MinPeakDistance you get:
The code I used to generate the above plot:
y = dlmread('freq.csv', ' ');
[peak_y, peak_x] = findpeaks(y(:,7), "MinPeakDistance", 40);
plot(y(:,1), y(:,7), y(peak_x,1), peak_y, '.r');
Depending on what you want findpeaks() might be enough, see help findpeaks and demo findpeaks for other options you can tweak.
It's a bit of tweaking but this example should help:
y2=y1=y0=NaN
stat "data/freq.csv" using (y2=y1,y1=y0,y0=$7,(y1>y2&&y1>y0?y1:NaN)) prefix "data"
Now in the variable data_records you should get the COUNT of local maximums you have in column 7.
You can print via
print data_records
To understand more, I post here an example of the sinus function
set table 'test.dat'
plot sin(x)
unset table
x2=x1=x0=NaN
y2=y1=y0=NaN
plot 'test.dat' using (x2=x1,x1=x0,x0=$1,x1):(y2=y1,y1=y0,y0=$2,(y1>y2&&y1>y0?y1:NaN)) w p, 'test.dat' u 1:2 w l
Should plot a sinus and also the maximum points.
In case several points have the same value:
x2=x1=x0=NaN
y2=y1=y0=NaN
plot 'freq.csv' u 0:7 w l, '' using (x2=x1,x1=x0,x0=$0,x1):(y2=y1,y1=y0,y0=$7,(y1>=y2&&y1>y0?y1:NaN)) w p
or
plot 'freq.csv' u 0:7 w l, '' using (x2=x1,x1=x0,x0=$0,x1):(y2=y1,y1=y0,y0=$7,(y1>y2&&y1>=y0?y1:NaN)) w p
depending on which side of the plateau you want to count the peak
The stat command becomes:
stat 'freq.csv' using (y2=y1,y1=y0,y0=$7,(y1>=y2&&y1>y0?y1:NaN)) prefix "data"

Plotting fit in gnuplot log($1):($2) defining xrange

I have a question regarding plotting a exponential fit in Gnuplot and having the y-axis set in logscale. I am using Gnuplot 4.6.3.
I will present the few ways I have tried setting a range to fit in:
The function I want to fit on the data is f(x)=a+b*x
The first:
set log y
f(x)=a+b*x
fit [4:8] f(x) 'CT2A_OH_R_log.dat' using (log($1)):($2) via a,b
Resutls:
Read 15 points
Skipped 15 points outside range [x=4:8]
No data to fit
error during fit
The second:
fit f(x) 'CT2A_OH_R_log.dat' using (log($1)):($2) via a,b
Results:
Final set of parameters Asymptotic Standard Error
a =16.9858 +/- 6.299 (37.08%)
b =-8.43215 +/- 3.502 (41.53%)
gnuplot> plot 'CT2A_OH_R_log.dat', f(x) via a,b
Obviously the fit is ridiculous but this is the only way I know of that actually gives me a fit. I know the problem most likely lies within the defining of the xrange when fitting but what else can I write there to fix this?
Below I will show image of my plot (f(x) is not showing as it is plotted in the negative area.)
'cause a logscale of the y axis requires the logarithm of the y axis during the fit ;-)
fit [4:8] f(x) 'CT2A_OH_R_log.dat' u 1:(log($2)) via a,b
NOTE
If you want to use the logarithm based on 10, you have to use log($2)/log(10) or log10($2).
#Tom Solid
gnuplot> fit [4:8] [0.1:60] f(x) 'CT2A_OH_R_log.dat' using 1:(log($2))
via a,b
gnuplot> plot 'CT2A_OH_R_log.dat', f(x) via a,b
This time the fit is exponential like.
So, I will post the picture of what I am getting now:

How to plot data from different blocks with lines in Gnuplot?

I have a data file with blocks of x/y values. Each block contains 16 lines with x/y pairs and each block represents those positions in a different time. http://pastebin.com/0teRrfRU
I want to plot the trajectory of a specific particle. To do that, I've written plot 'pos.dat' u 2:3 every ::n:0:n:i, where n is the n-th particle and i is the time up to which I want the trajectory plotted (I can then loop over the i to generate an animation).
This runs fine, but when I add w lines nothing gets plotted, and I don't understand why. Is there a way to plot this with lines? The only alternative I see is writing a script to parse the data file and generate a new one with only the values I want (effectively acting as every), but I don't want to do that if I can do it in Gnuplot.
After a closer look to your data, your case has some speciality.
Like in Plotting same line number of several blocks data with gnuplot you can plot the file into a table via with table which will remove the empty lines and hence lines will be connected.
However, some of your particles disappear on one side and re-appear on the opposite side. If you plot this with lines you will get a line through the whole graph which is certainly undesired. You can workaround this if you introduce a function Break() which returns NaN if the difference of two successive x- or y-values are larger than 90% (to be on the safe side) of the x- or y-range , respectively. The effect of NaN is that the line will interrupted.
Code: (works with gnuplot>=5.0.0 version at the time of OP's question)
### plotting trajectories
reset session
set term gif animate delay 3 size 400,400
set output "SO30744875.gif"
set size square
FILE = 'SO30744875.dat'
set key noautotitle
stats FILE u (N=column(-1),M=column(1),$2):3 nooutput
xrange = STATS_max_x-STATS_min_x
yrange = STATS_max_y-STATS_min_y
set table $Data
plot FILE u 1:2:3 w table
unset table
Break(col1,col2) = (x0=x1,x1=column(col1), y0=y1,y1=column(col2), \
abs(x1-x0)<0.9*xrange && abs(y1-y0)<0.9*yrange ? column(col2) : NaN)
do for [i=0:N] {
plot for [j=1:16] x1=y1=NaN $Data u 2:(Break(2,3))every M::j-1::(i+1)*M w l, \
FILE u 2:3 every :::i::i w p pt 7, \
FILE u 2:3:1 every :::i::i w labels offset 0.7,0.7
}
set output
### end of code
Result:

How to make gnuplot give you the coefficients of a data fit

I'm using gnuplot to fit data to a certain function with unknown coefficients. I can get gnuplot to plot the fitted function, but I can't figure out how to have gnuplot give me the actual equation of that function.
The main command is:
f(x) = a/(1+(b*(x-c)*(x-c)))
a=80
b=.001
c=70
fit f(x) "data.dat" using 1:2:3 via a,b,c
I want gnuplot to tell me the values it comes up with for a b and c. Or maybe I am misinterpreting this command and it is actually using the values I plugged in as a,b, and c. Can anyone help me out here?
After fitting the values are stored in the respective variables a, b and c
f(x) = a/(1+(b*(x-c)*(x-c)))
a=80
b=.001
c=70
fit f(x) "data.dat" using 1:2:3 via a,b,c
print a, b, c
plot "data.dat" w p, f(x) title sprintf("a=%e, b=%e, c=%e", a, b, c)

Resources