Hello Daniel,
The integral over the normal distribution (=:dist1) from minus infinity to infinity is 1. The integral over the curve you get from histfit (=:dist2) from minus infinity to infinity is equal to the number of data points times the length of one bin. This is where you get the scaling factor from. Now there are two possibilities, you can either scale dist1 up or dist2 down.
Solution1: scale dist1 up
n = 1000;
data = randn(1,1000);
nbins = 20;
h = histfit(data,nbins);
hold on;
mu = 0;
sigma = 1;
xx = linspace(min(data),max(data),500);
yy = normpdf(xx,mu,sigma);
X = get(h(1),'XData');
lengthBin = X(3,1)-X(2,1);
scalingFactor = n * lengthBin;
plot(xx,scalingFactor*yy,'g','Linewidth',get(h(2),'LineWidth'));
legend('histplot','fitted distribution','real distribution');
result:
Solution 2: scale dist2 down
Sorry, the code posted here is kind of a workaround, because the thing I really wanted to do didn't work. I will post the not working solution below and it would be really kind, if someone could tell me why it doesn't work.
n = 1000;
data = randn(1,1000);
nbins = 20;
h = histfit(data,nbins);
mu = 0;
sigma = 1;
xx = linspace(min(data),max(data),500);
yy = normpdf(xx,mu,sigma);
X = get(h(1),'XData');
Y = get(h(1),'YData');
xFitDist = get(h(2),'XData');
yFitDist = get(h(2),'YData');
centers = unique(mean(X,1));
counts = Y(2,:);
lengthBin = X(3,1)-X(2,1);
scalingFactor = n * lengthBin;
bar(centers,counts/scalingFactor,1)
hold on;
plot(xFitDist,yFitDist/scalingFactor,'r','Linewidth',3);
plot(xx,yy,'g','Linewidth',3);
legend('histplot','fitted distribution','real distribution');
result:
Solution 3: not working
n = 1000;
data = randn(1,1000);
nbins = 20;
h = histfit(data,nbins);
mu = 0;
sigma = 1;
xx = linspace(min(data),max(data),500);
yy = normpdf(xx,mu,sigma);
X = get(h(1),'XData');
lengthBin = X(3,1)-X(2,1);
scalingFactor = n * lengthBin;
set(h(1),'YData',get(h(1),'YData')/scalingFactor);
set(h(2),'YData',get(h(2),'YData')/scalingFactor);
hold on;
plot(xx,yy,'g','Linewidth',get(h(2),'Linewidth'));
legend('histplot','fitted distribution','real distribution');
Here is the resulting figure, which obviously is wrong:
I hope I was able to help you and my answer is correct, as this is my first answer here. I would be really glad, if someone could tell me, why the third code is not working (the plot is ugly).
Have a nice day Lukas