Fitting a function to a histogram created with frequency in gnuplot

Introduction

Gnuplot has a solution for creating a histogram from a file called hist.dat , which is like

 1 2 2 2 3 

using commands

 binwidth=1 set boxwidth binwidth bin(x,width)=width*floor(x/width) + binwidth/2.0 plot [0:5][0:*] "hist.dat" u (bin($1,binwidth)):(1.0) smooth freq with boxes 

which generates a histogram like this from another SO page .

Question

How can I put my function on this histogram? I defined a Gaussian function and initialized its values ​​to

 f(x) = a*exp(-((xm)/s)**2) a=3; m=2.5; s=1 

and the output function is well suited to the histogram.

Unfortunately, I cannot approach this histogram using the command

 fit f(x) "hist.dat" u (bin($1,binwidth)):(1.0) smooth freq via a,m,s ^ Need via and either parameter list or file 

So how can I put my function without creating a new file containing binned values?

+4
source share
2 answers

I ran into a similar problem and I found some very gentle solution.

 binwidth=1 set boxwidth binwidth bin(x,width)=width*floor(x/width) + binwidth/2.0 set table 'hist.temp' plot [0:5][0:*] "hist.dat" u (bin($1,binwidth)):(1.0) smooth freq with boxes unset table 

And then you can fit the file as you wish. I know that there may be a better way to do this, but for me it is a quick and effective solution. Hope this will be helpful for you.

Hooray!

+3
source

I used this and worked:

 gauss(x)=a/(sqrt(2*pi)sigma)*exp(-(x-mean)**2/(2*sigma**2)) fit gauss(x) 'data.txt' via a,sigma,mean 

after 83 iterations, GNUplot calculated me a, sigma and mean

+1
source

All Articles