MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Histogram

Asked by Mate 2u on 19 Feb 2012

Hi everybody. I have an array.

1) I do a histfit(data) to get a histogram representation of my data with a normal distribution curve on top. I want a 3rd thing on this graph and that is a smooth line representing the data (so I want the curve representing the distribution of my data on top of this to compare with the normal distribution)

Thanks.

Products

No products are associated with this question.

Answer by Tom Lane on 19 Feb 2012

There is a ksdensity function that can produce a kernel-smooth density estimate. The issue is that it produces a density (integrates to 1) and the histogram is not a density (bar heights sum to 1). You could figure out the area of the histogram and re-scale the ksdensity values. Alternatively, here's a way to create the histgram, normal curve, and kernel density separately:

```x = [randn(100,1); 4+randn(50,1)];
[hts,ctrs] = hist(x)
bar(ctrs,hts,'hist')
area = sum(hts) * (ctrs(2)-ctrs(1))
xx = linspace(-3,7);
hold on; plot(xx,area*normpdf(xx,mean(x),std(x)),'r-')
f = ksdensity(x,xx);
plot(xx,area*f,'g-')
hold off
```

tiago on 11 Sep 2013

Hi, thanks a lot for the help with the code There is any way to know the r^2 of the normal curve?