Plot - add legend individually

조회 수: 16 (최근 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 CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by