Labelling multiple plots of the same color with a single legend label.

18 ビュー (過去 30 日間)
MH
MH 2023 年 1 月 6 日
コメント済み: Voss 2023 年 1 月 6 日
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!

採用された回答

Voss
Voss 2023 年 1 月 6 日
編集済み: Voss 2023 年 1 月 6 日
You can set the HandleVisibility to 'off' in one of each pair of plot calls.
figure
hold on
plot(1:10,1:10,'DisplayName','1','Color','b')
plot(1:10,2:11,'HandleVisibility','off','Color','b')
legend
  2 件のコメント
MH
MH 2023 年 1 月 6 日
Thank you! That was a much simpler fix than the stuff I was trying to do.
Voss
Voss 2023 年 1 月 6 日
You're welcome!

サインインしてコメントする。

その他の回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 1 月 6 日
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
...

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by