0

I'm trying to fit data (histogram) in gnuplot. I tried various functions, and by looking at my histogram, I suppose the best fit is lognormal or gamma distribution, but I am not able to do this fit in gnuplot (Im rather new user of gnuplot).

Here is picture of histogram with gaussian distribution:

Histogram with gauss distribution (bad fit)

Also here is code in gnuplot:

reset
n=100 #number of intervals
max=15. #max value
 min=0. #min value
 width=(max-min)/n #interval width
#function used to map a value to the intervals
 hist(x,width)=width*floor(x/width)
 set term png #output terminal and file
 set output "histogram.png"
 set xrange [min:max]
 set yrange [0:]
#to put an empty boundary around the
#data inside an autoscaled graph.
 set offset graph 0.05,0.05,0.05,0.0
 set xtics min,(max-min)/5,max
 set boxwidth width*0.9
 set style fill solid 0.5 #fillstyle
 set tics out nomirror
 set xlabel "Diameter"
 set ylabel "Frequency"
#count and plot

#fac(x) = (int(x)==0) ? 1.0 : int(x) * fac(int(x)-1.0)
 gauss(x)=a/(sqrt(2*pi)*sigma)*exp(-(x-mean)**2/(2*sigma**2))
 fit gauss(x) 'hist.temp' u 1:2 via a, sigma, mean


 plot 'data.list' u (hist($8, width)):(1.0) smooth freq w boxes lc           rgb     "green" notitle, \
  gauss(x) w lines ls 2 lw 2

In file hist.temp is tabular output ( see this link )

Community
  • 1
  • 1
viktor.radovic
  • 508
  • 2
  • 9
  • 20
  • If you know that the distribution should be a lognormal (which your underlying model should tell you), then define a function `lognormal(...)`, see e.g. http://gnuplot.sourceforge.net/demo/prob.31.gnu, and use that instead of the `gauss` to do the fitting. – Christoph Jan 09 '17 at 16:25
  • Im obtaining singular matrix invsqrt2pi = 0.398942280401433 lognormal(x)=x<0?0.0:invsqrt2pi/sigma/x*exp(-0.5*((log(x)-mu)/sigma)**2) fit lognormal(x) 'hist.temp' u 1:2 via sigma, mu – viktor.radovic Jan 11 '17 at 02:50
  • That can be an indication, that the function doesn't fit at all. The first reason is probably, that the lognormal function you use is a PDF, i.e. normalized, whereas your data isn't. Depending on how you actually generate your data, you could already include this step there. – Christoph Jan 11 '17 at 06:40

0 Answers0