Labelling multiple plots of the same color with a single legend label.
32 views (last 30 days)
Show older comments
Hello everyone,
I am trying to plot multiple sets of data on a single plot within a while statement. I have two sets of data I plot for each "while value" and I want those sets to be the same color when I plot them which I was able to do but I also want to label them as the same thing in a legend without having the labelling repeat. Currently the way I have formatted the code, the label repeats for both sets of data as shown in my code below. If someone could help out with this that would be great, thank you!

0 Comments
Accepted Answer
More Answers (1)
Sulaymon Eshkabilov
on 6 Jan 2023
In your exerise, there are a coupole of different ways to do that.
(1) An easier one is to combine your data and plot them once the simulation is complete.
...
ii=0;
N = 11;
while ii <N
x(ii)=...
y(ii) = ...
ii = ii+1;
end
plot(x, y, 'DisplayName', 'x vs. y')
legend show
...
(2) Skip the legend in the second round, setting legend 'off', e.g.:
...
while ii<N
plot(x(ii), y(ii), 'DisplayName', 'x vs. y')
hold on
h = plot(x(ii+1), y(ii+1), 'DisplayName', 'x+1 vs. y+1');
h.Annotation.LegendInformation.IconDisplayStyle = 'off';
ii=ii+1;
end
legend show
...
0 Comments
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!