
Legend and Colors for Iteration function
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
Adam Danz
am 14 Sep. 2020
Bearbeitet: Adam Danz
am 14 Sep. 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 Kommentare
Adam Danz
am 15 Sep. 2020
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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Legend finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

