Why am I getting the same colour of the legend for the last 2 plots?

7 ビュー (過去 30 日間)
Dev
Dev 2023 年 3 月 28 日
コメント済み: Star Strider 2023 年 3 月 29 日

採用された回答

Star Strider
Star Strider 2023 年 3 月 28 日
編集済み: Star Strider 2023 年 3 月 28 日
I suspect that it is because ‘y2’ has more than one row.
The solution is to return the handles of the plots, and select only one line from the second plot in the legend call —
H = 1:25;
y1 = rand(1,25)+0.25;
y2 = rand(2,25)+0.5;
F = rand(2,25);
figure
plot(H, y1, '-b');
hold on
plot(H, y2, '-g');
plot(H, F(1,:), 'r-');
hold off
legend('Line1','Line2','Line3', 'Location','best')
title('Original')
figure
hp1 = plot(H, y1, '-b');
hold on
hp2 = plot(H, y2, '-g');
hp3 = plot(H, F(1,:), 'r-');
hold off
legend([hp1;hp2(1);hp3],'Line1','Line2','Line3', 'Location','best')
title('Corrected')
EDIT — (28 Mar 2023 at 16:37)
Wrote ‘column’, intended ‘row’ in the first sentence. Fixed now.
.
  2 件のコメント
Dev
Dev 2023 年 3 月 29 日
Thank you!
Star Strider
Star Strider 2023 年 3 月 29 日
As always, my pleasure!

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

その他の回答 (1 件)

Mathieu NOE
Mathieu NOE 2023 年 3 月 28 日
add one more line
at the end of your script, add :
hold off

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by