MATLAB Answers

Legend Color doesn't Match the Curves Color

7 views (last 30 days)
A.J.M
A.J.M on 15 Sep 2020
Commented: A.J.M on 15 Sep 2020
Good Day;
Here my question is happened often to occur as I see when I searched to solve my problem,but each case it depend on their code. I faced this problem as shown in the attached figure, and I hope to find the solution from you. I used 'hold on'but nothing changed, the code as shown below:
Z=100:250:5000; %Distance Between The Transmitter and Reciever in m
pointing_Err_angle=1*10^-5; %Pointing Error Angle in mrad
lambda=1550e-9; %Wavelength in nm
wo=5*10^-2; %Beam Waist at Z=0 in m
Cn=sqrt(5e-15); %Refractive Index Structure Parameter
% --------------------Calculation Section----------------------------------
k=2*pi/lambda; %The number of optical wave
po=(0.55*(Cn)^2*k^2*Z).^(-3/5); %The Coherence Length
E=(1+2*wo^2./po.^2);
wz=wo*(1+E.*((lambda.*Z)./(pi*wo^2)).^2).^(1/2); %The Beam Waist
r=Z*(pointing_Err_angle); %The Radial Displacement B
%----------------------------------------Calculate Hp-----------------------------
v=(sqrt(pi)*a)./(sqrt(2)*wz);
a0=(erf(v)).^2; %Maximal Fraction of Collected Power at r=0
Wzeq=sqrt(wz.^2*((sqrt(pi).*erf(v))/(2*v.*exp(-v.^2))));
Hp=a0.*exp((-2*r.^2)./((Wzeq).^2));
%----------------------------------Intensity Distribution-------------------------
Rytov_var=[0.1,0.2,0.5,0.8]; %Log irradiance variance (Roytov variance)
for i=1:length(Rytov_var)
for j=1:5:length(Hp)
A1(i,j)=1./(Hp(j).*sqrt(2*pi*Rytov_var(i)));
A2(i,j)=((log(Hp(j))+0.5.*Rytov_var(i)).^2)./2.*Rytov_var(i).^2;
PDF_Hp(i,j)=A1(i,j).*exp(-(A2(i,j)));
end
end
%--------------------------Plot Section------------------------------------
z=1250:1250:5000; %Distance for Plot Purpose
figure(1)
semilogy(z,PDF_Hp,'LineWidth',4);
xlabel('The Distance Z')
ylabel('The Probability Density Function ')
title('The PDF of Hp Vs The Distance')
legend('σ^2_I=0.1','σ^2_I=0.2','σ^2_I=0.5','σ^2_I=0.8')
grid on
figure(2)
semilogy(Rytov_var,PDF_Hp,'LineWidth',3);
xlabel('Roytov variance')
ylabel('The Probability Density Function ')
title('The PDF of Hp Vs Rytove Variance')
legend('σ^2_I=0.1','σ^2_I=0.2','σ^2_I=0.5','σ^2_I=0.8')
grid on

  0 Comments

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 15 Sep 2020
Edited: Adam Danz on 15 Sep 2020
I see the problem. Look at the values you're plotting. Assuming a=0.5e-2;
semilogy(z,PDF_Hp,'LineWidth',4);
PDF_Hp =
Columns 1 through 12
59.204 0 0 0 0 74.572 0 0 0 0 130.59 0
33.684 0 0 0 0 41.243 0 0 0 0 66.92 0
5.2821 0 0 0 0 5.3479 0 0 0 0 5.1881 0
0.42448 0 0 0 0 0.30818 0 0 0 0 0.12088 0
Columns 13 through 16
0 0 0 242.89
0 0 0 112.93
0 0 0 4.5326
0 0 0 0.032869
A line is created for each column so your plot actually has 16 lines, not 4.
The reason they don't appear is because in a log plot, 0 is not defined. However, those line objects are still produced - they are just not shown. As evidence of that, check out the 16 object handles.
h = semilogy(z,PDF_Hp,'LineWidth',4)
% h =
% 16×1 Line array:
%
% Line
% Line
% Line
% Line
% Line
% Line
% Line
% Line
% Line
% Line
% Line
% Line
% Line
% Line
% Line
% Line
The reason the "wrong" colors are defined is because you're only asking for the first 4 objects to appear in the legend. But objects 2-3-4 are all non-visible lines.
legend('σ^2_I=0.1','σ^2_I=0.2','σ^2_I=0.5','σ^2_I=0.8')
Solution
Get rid of the columns with 0s in PDF_Hp

  1 Comment

A.J.M
A.J.M on 15 Sep 2020
Thanks a lot, Mr. Adam Danz, the problem was solved. I appreciate your hard efforts
Accept my respect.

Sign in to comment.

More Answers (3)

Bjorn Gustavsson
Bjorn Gustavsson on 15 Sep 2020
That's peculiar. What happens if you keep the plot-handle from your calls to semilogy and send that to legend?
ph = semilogy(z,PDF_Hp,'LineWidth',4);
xlabel('The Distance Z')
ylabel('The Probability Density Function ')
title('The PDF of Hp Vs The Distance')
legend(ph,'σ^2_I=0.1','σ^2_I=0.2','σ^2_I=0.5','σ^2_I=0.8')
That will at least allow you to modify the line-colors and check how they change in the plot and in the legend.
HTH

  1 Comment

A.J.M
A.J.M on 15 Sep 2020
I did that, but the same result as shown in the attached figure.
figure(1)
ph=semilogy(z,PDF_Hp,'LineWidth',4);
xlabel('The Distance Z')
ylabel('The Probability Density Function ')
title('The PDF of Hp Vs The Distance')
legend(ph,'σ^2_I=0.1','σ^2_I=0.2','σ^2_I=0.5','σ^2_I=0.8')
grid on

Sign in to comment.


Vasishta Bhargava
Vasishta Bhargava on 15 Sep 2020
Edited: Vasishta Bhargava on 15 Sep 2020
% if true
% code
%end
ph = semilogy(z,PDF_Hp,'LineWidth',4);
xlabel('The Distance Z')
ylabel('The Probability Density Function ')
title('The PDF of Hp Vs The Distance')
legend(ph,{'σ^2_I=0.1','σ^2_I=0.2','σ^2_I=0.5','σ^2_I=0.8'})
Pass it as cell array

  2 Comments

Vasishta Bhargava
Vasishta Bhargava on 15 Sep 2020
I see you have multiple script files open in editor. Use
%if true
% code
% end
close all
In the beginning of this file to close any open figure data which you have plotted before

Sign in to comment.


A.J.M
A.J.M on 15 Sep 2020
Edited: A.J.M on 15 Sep 2020
Is there no one who can solve my problem in the MATLAB forum itself. Where are the experts!!!

  0 Comments

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