How can I use various colors to fill the area under a normal distribution curve?

조회 수: 26 (최근 30일)
Hi, I obtained a probability distribution for my data using the following syntax:
h = histfit(data);
and then:
pd = fitdist(data,'Normal');
I want to create a graph similar to Figure 1 using the mean and standard deviation values obtained from the histogram and fitted distribution.
In advance, I would like to express my gratitude for your assistance.
Figure 1:

채택된 답변

Star Strider
Star Strider 2023년 11월 28일
Sure!
Try this —
x = linspace(1.5, 5, 250);
mu = 3.3;
s = 0.6;
y = normpdf(x, mu, s);
figure
plot(x, y)
hold on
Lv1 = (x>=mu) & (x<mu+s);
patch([x(Lv1) flip(x(Lv1))], [zeros(size(y(Lv1))) flip(y(Lv1))], 'g', 'EdgeColor','none')
Lv2 = (x>=mu+s) & (x<mu+2*s);
patch([x(Lv2) flip(x(Lv2))], [zeros(size(y(Lv2))) flip(y(Lv2))], 'y', 'EdgeColor','none')
Lv3 = (x>=mu+2*s) & (x<mu+3*s);
patch([x(Lv3) flip(x(Lv3))], [zeros(size(y(Lv3))) flip(y(Lv3))], 'r', 'EdgeColor','none')
hold off
If you want the text annotations as well, I can supply those, and the vertical lines, too.
.
  댓글 수: 6
Navid
Navid 2023년 11월 28일
Thank you for your assistance. The second option was correctly implemented in my version. Thank you again.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Angelo Yeo
Angelo Yeo 2023년 11월 28일
f = @(x, mu, sd) 1/(sd*sqrt(2*pi)) * exp(-1/2*((x-mu)/sd).^2);
x = linspace(1.5, 5, 1000);
mu = 3.25; sd = 0.5;
figure;
hold on;
x_zone1 = linspace(mu, mu+sd, 300);
x_zone2 = linspace(mu+sd, mu+2*sd, 300);
x_zone3 = linspace(mu+2*sd, mu+3*sd, 300);
area(x_zone1, f(x_zone1, mu, sd), 'FaceColor', 'g')
area(x_zone2, f(x_zone2, mu, sd), 'FaceColor', 'y')
area(x_zone3, f(x_zone3, mu, sd), 'FaceColor', 'r')
plot(x, f(x, mu, sd), 'color','k', 'linewidth', 2);

Chunru
Chunru 2023년 11월 28일
편집: Chunru 2023년 11월 28일
data = randn(8192, 1);
h = histfit(data);
pd = fitdist(data,'Normal')
pd =
NormalDistribution Normal distribution mu = -0.00241653 [-0.0240676, 0.0192345] sigma = 0.999681 [0.984606, 1.01523]
figure;
x = linspace(-4*pd.sigma, 4*pd.sigma, 1001);
p = pdf(pd, x);
figure;
plot(x, p);
hold on
facecolor =["g", "y", "r"]
facecolor = 1×3 string array
"g" "y" "r"
for i=1:3
idx = (x >= (i-1)*pd.sigma) & (x < i*pd.sigma);
area(x(idx), p(idx), FaceColor=facecolor(i))
end
  댓글 수: 3
Chunru
Chunru 2023년 11월 28일
It seems that you are using older version of matlab. Use the following name-value syntax for older version matlab.
area(x(idx), p(idx), 'FaceColor', facecolor(i))
Navid
Navid 2023년 11월 28일
Dear Chunru
I wanted to express my gratitude for the assistance you provided. I successfully modified your code and achieved my desired outcome thanks to your guidance. I appreciate your help.
mu = pd.mu; sd = pd.sigma;
x = linspace(mu-4*pd.sigma,mu+4*pd.sigma,1001);
p = pdf(pd,x);
figure;
plot(x,p);
hold on
facecolor=['g','y','r'];
for i=1:3
idx = (x>= mu+(i-1)*pd.sigma)&(x<mu+i*pd.sigma);
area(x(idx), p(idx), 'FaceColor', facecolor(i))
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by