Plot - add legend individually

29 ビュー (過去 30 日間)
RuiQi
RuiQi 2017 年 7 月 13 日
コメント済み: ROBIN KUMAR 2022 年 2 月 18 日
How can I update the legend of each plot individually rather than at the end of the code ?
plot(x1, y1, '-vr', 'LineWidth', linewidth, 'MarkerSize', markersize);
legend('Algorithm 1');
plot(x2, y2, '-vr', 'LineWidth', linewidth, 'MarkerSize', markersize);
legend('Algorithm 2');
The code above overwrites the legend for Algorithm 1
  1 件のコメント
ROBIN KUMAR
ROBIN KUMAR 2022 年 2 月 18 日
The same problem I have been facing with R2021a version. Please help me execute the same..

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

採用された回答

Adam
Adam 2017 年 7 月 13 日
編集済み: Adam 2017 年 7 月 13 日
If you are working with a fairly recent version of Matlab (> R2014b I think) then you can edit an existing legend if you keep the handle to it:
figure; hAxes = gca;
plot(hAxes, x1, y1, '-vr', 'LineWidth', linewidth, 'MarkerSize', markersize);
hLegend = legend( hAxes, 'stuff' );
hold( hAxes, 'on' )
plot(hAxes, x2, y2, '-vr', 'LineWidth', linewidth, 'MarkerSize', markersize);
hLegend.String{2} = 'other stuff';
It is more efficient to do all the plots first and then put the legend up for all of them though.
  3 件のコメント
Gopal New
Gopal New 2020 年 11 月 30 日
編集済み: Gopal New 2020 年 11 月 30 日
Hi,
when i run the above code, i only get one legend. i'm trying to do something similar, but can't make it work. i'm using R2016.
ROBIN KUMAR
ROBIN KUMAR 2022 年 2 月 18 日
This doesn't work at all. The same problem I have been facing with R2021a version. Please help me execute the same..

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

その他の回答 (1 件)

ROBIN KUMAR
ROBIN KUMAR 2022 年 2 月 18 日
The same problem I have been facing with R2021a version. Please help me execute the same..

カテゴリ

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