Legend and Colors for Iteration function

조회 수: 4 (최근 30일)
A.J.M
A.J.M 2020년 9월 14일
댓글: A.J.M 2020년 9월 15일
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

채택된 답변

Adam Danz
Adam Danz 2020년 9월 14일
편집: Adam Danz 2020년 9월 14일
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
Adam Danz
Adam Danz 2020년 9월 15일
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
A.J.M
A.J.M 2020년 9월 15일
Thanks a lot, Mr. Adam Danz, the problem was solved. I appreciate your hard efforts
Accept my respect.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by