Legend and Colors for Iteration function

Good Day;
I need the steps that I can add Legend to my iteration function, where there are four curves, therefore, for each curve there must have specific color and legend that point to it.
Many Thanks

 Risposta accettata

Adam Danz
Adam Danz il 14 Set 2020
Modificato: Adam Danz il 14 Set 2020
Use the displayname property to assign the legend string and use the "color" property to set the line color.
% Define colors
n = 12;
colors = jet(n);
% Plot within loop
hold on % Important!
for i = 1:n
plot(1:5, rand(1,5), '-', 'Color', colors(i,:),...
'DisplayName', sprintf('Line #%d',i));
end
legend()

14 Commenti

Dear Mr.Adam Danz ;
the result as shown in the figure, and my code as shown below, where is my mistake please.
n = 4;
colors = jet(n);
hold on
figure(1)
for i=1:length(Rytov_var)
for j=1:length(Hp)
plot(Z, PDF_Hp, '-', 'Color', colors(i,:),...
'DisplayName', sprintf('Line #%d',i));
end
end
grid on
legend('σ^2_I=0.1','σ^2_I=0.2','σ^2_I=0.5','σ^2_I=0.8')
Adam Danz
Adam Danz il 14 Set 2020
Modificato: Adam Danz il 14 Set 2020
  1. Use numel() instead of length().
  2. See the 4 "% <------changed" comments
  3. Am I correct that the i-loop controls the color and the j-loop controls the legend string? Where are those values (=0.1, =0.2, etc) coming from? If those are variable values, use those variable to assign the numbers rather than hard-coding them. There's no sense in using DisplayName and defining the legend strings in legend().
n = numel(Rytov_var); % <------changed
colors = jet(n);
hold on
figure(1)
for i=1:n % <------changed
for j=1:numel(Hp) % <------changed
plot(Z, PDF_Hp, '-', 'Color', colors(i,:),...
'DisplayName', sprintf('σ^2_I=%.1f',i)); % ?????????
end
end
grid on
legend()% <------changed
A.J.M
A.J.M il 14 Set 2020
Modificato: A.J.M il 14 Set 2020
Mr. Adam Danz;
Where are those values (=0.1, =0.2, etc) coming from? Yes these value are varibles,and it belong to Rytov_var varible. I assigned it but the same result.
Ok, then
plot(Z, PDF_Hp, '-', 'Color', colors(i,:),...
'DisplayName', sprintf('σ^2_I=%.1f',Rytov_var(i)));
% ^^^^^^^^^^^^
but that will result in duplicate colors and legend strings. Since I don't know what the two loops are supposed to do, I cannot suggest an alternative. Perhaps the 1st loop controls the legend string and the 2nd loop controls the color? If so,
n = numel(Hp);
colors = jet(n);
hold on
figure(1)
for i=1:numel(Rytov_var)
for j=1:n
plot(Z, PDF_Hp, '-', 'Color', colors(j,:),...
'DisplayName', sprintf('σ^2_I=%.1f',Rytov_var(i)));
end
end
grid on
legend()
A.J.M
A.J.M il 14 Set 2020
Modificato: Adam Danz il 14 Set 2020
I'm sorry if I disturb you. The result as shown in the figure. Below is the original function that I want to plot it. The plot between PDF_Hp and the distance Z. I appreciate your efforts.
Z=100:250:5000; %Distance Between The Transmitter and Reciever in m
Rytov_var=[0.1,0.2,0.5,0.8];
for i=1:length(Rytov_var)
for j=1: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);
PDF_Hp(i,j)=A1(i,j).*exp(-(A2 (i,j)));
end
end
Take a few moments to understand what your loops are doing. I don't know what they are supposed to do and I can't run the code since there are missing variable values (e.g. Hp).
If you understand how my solution assigns color and assigns the legend string, I think you'll figure out what needs changed in your code. But since I don't understand what the loops are for and how color is supposed to be assigned and how the legend string is supposed to be assigned, I can't propose a solution.
Thanks a lot Mr. Adam Danz.
No problem, I'd be glad to help further if needed.
That's very kind of you Mr. Adam Danz.
Accept my respect
Good day Mr. Adam Danz;
This is my code, I plotted without loop, the new problem is the legend colors don't match the curves.
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
I cannot run the full code because "a" is not defined.
v=(sqrt(pi)*a)./(sqrt(2)*wz);
Error:
Unrecognized function or variable 'a'.
Sorry for this mistake;
a=0.5e-2;
I see the problem. Look at the values you're plotting.
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
Thanks a lot, Mr. Adam Danz, the problem was solved. I appreciate your hard efforts
Accept my respect.

Accedi per commentare.

Più risposte (0)

Richiesto:

il 14 Set 2020

Commentato:

il 15 Set 2020

Community Treasure Hunt

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

Start Hunting!

Translated by