Incomplete Normal Distribution Plot on MATLAB

4 views (last 30 days)
Abdul Moeez Baig
Abdul Moeez Baig on 17 Oct 2021
Commented: Dave B on 17 Oct 2021
There is alot of data that has negative and positive numbers both. I am unsure if I am using the function properly. I want to plot normal distribution of the curve however for some plots, the curve is not symmetric (isn't bell shaped) and even if I include the rest of the axis, it doesnt exist. I want to know where I am making the mistake..
data = sort(data);
data = data(~isnan(data));
uu = mean(data,'omitnan');
sigma = std(data,'omitnan');
y=normpdf(data,uu,sigma);
plot(data,y);

Answers (1)

Dave B
Dave B on 17 Oct 2021
The function normpdf(x,mu,sigma) will return the pdf of the normal distribution with mean mu and standard deviation sigma, evaluated at the values in x.
x=linspace(-5,5,100);
mu=0;
sigma=1;
y=normpdf(x,mu,sigma);
plot(x,y)
If your data are very non normal, this wouldn't look very bell shaped, because you'd be plotting it based on x but x wouldn't sample the area around the mean. Here's an extreme case to illustrate:
x=[repelem(1,100) 2:10];
y=normpdf(x,mean(x),std(x));
plot(x,y)
title(sprintf('mean(x) = %0.2f; std(x) = %0.2f', mean(x), std(x)))
  2 Comments
Dave B
Dave B on 17 Oct 2021
Sorry I should have included some more words in my answer. You're plotting a normal distribution, a curve, which is by all means symmetrical and bell shaped, but you're plotting it for a set of x which does not sample that region. It looks to me like your data do not include values less than 0, if you asked MATLAB min(data) I suspect it would be 0.
It's hard to say if this represents an error in your code, or your goals, or if this is what you should expect. You could visualize more of the curve by plotting:
data = sort(data);
data = data(~isnan(data));
uu = mean(data,'omitnan');
sigma = std(data,'omitnan');
x = linspace(-1000,1000,100)
y=normpdf(x,uu,sigma);
plot(x,y);
Or if you prefer you could define x based on uu and sigma, i.e. pick some number of standard deviations above and below the mean that you'd like to plot:
n_std = 5;
n_pts = 100;
x = linspace(uu - sigma * n_std,uu + sigma * n_std, n_pts)
...

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by