90 views (last 30 days)

Star Strider
on 24 Nov 2016

It’s easy enough to create your own version of histfit if you need to. See if this does what you want:

data = 0.5*randn(1,1000) + 2; % Create Data

mu = mean(data);

sd = std(data);

ndfcn = @(mu,sd,x) exp(-(x-mu).^2 ./ (2*sd^2)) /(sd*sqrt(2*pi)); % Standard Normal Distribution

[hc,edges] = histcounts(data, 25); % Histogram

ctrs = edges(1:length(edges)-1) + mean(diff(edges))/2; % Calculate Centres

ctrsx = linspace(min(ctrs), max(ctrs)); % High-Resolution Vector

sdnd = ndfcn(mu,sd,ctrsx); % Calculate Standard Normal Distribution

figure(1)

bar(ctrs, hc) % Plot Histogram

hold on

plot(ctrsx, sdnd*max(hc)/max(sdnd), '-r', 'LineWidth',2) % Plot Scaled Standard Normal Distribution

hold off

grid

Obviously, substitute your own data for my ‘data’ vector. I created mine to test my code.

Walter Roberson
on 24 Nov 2016

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/313862-how-to-plot-a-normal-distribution-graph-to-fit-a-bar-graph#comment_408871

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/313862-how-to-plot-a-normal-distribution-graph-to-fit-a-bar-graph#comment_408871

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/313862-how-to-plot-a-normal-distribution-graph-to-fit-a-bar-graph#comment_408895

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/313862-how-to-plot-a-normal-distribution-graph-to-fit-a-bar-graph#comment_408895

Sign in to comment.