
Legend and Colors for Iteration function
2 ビュー (過去 30 日間)
古いコメントを表示
採用された回答
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
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
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Legend についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

