How to set my legend differently from my plotting?

2 ビュー (過去 30 日間)
Leon
Leon 2022 年 2 月 21 日
コメント済み: Olaf Bousche 2022 年 2 月 22 日
I have a figure with 10 pairs of plots. Below is an example of one pair.
plot(x, y, 'r.', "LineWidth", 1);
p1 = pchip(x, y, xq);
plot(xq, p1, 'r-', "LineWidth", 1);
Basically, I first plot the dots and then their pchip line, thus having two symbols for each variable.
In my lengend, how do I have one symbol like the below:
'.-'
for each variable with a total of 10 legend inputs, instead of having 10 x 2 = 20 lengend inputs (dots and lines are treated as two separate inputs), as the default does?
Many thanks.

回答 (1 件)

Olaf Bousche
Olaf Bousche 2022 年 2 月 21 日
編集済み: Olaf Bousche 2022 年 2 月 21 日
You can use the line handles to do this. For each plot j store the handle:
p_handles(1) = plot(x, y, 'r.', "LineWidth", 1, "DisplayName","I want this");
p1 = pchip(x, y, xq);
p_handles(2) = plot(xq, p1, 'r-', "LineWidth", 1, "DisplayName","I do not want this");
Then when calling the legend
legend(p_handles(1:2:end),"show")
Different symbols won't work but when you stick to similar colors or something of that nature, things will be obvious for the reader.
  2 件のコメント
Leon
Leon 2022 年 2 月 21 日
Many thanks. When I tried it out, I got the below error:
Error using legend
Invalid argument. Type 'help legend' for more information.
Error in untitled (line 15)
legend(p_handles(1:2),"show")
Olaf Bousche
Olaf Bousche 2022 年 2 月 22 日
Sorry about that. I forgot the "hold on" and the "show" is not needed. Working example:
for j = 1:6
p(j) = plot(rand(10,1),"DisplayName",compose("plot %d",j));
hold on
end
legend(p(1:2:end))
Hope this works for you

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

カテゴリ

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

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by