How to add multiple legends in a loop for mulitple plots?

조회 수: 5 (최근 30일)
Naif Alsalem
Naif Alsalem 2022년 11월 13일
댓글: Naif Alsalem 2022년 11월 13일
Hi there,
I have, say, 3 plots generated in a loop like the following:
for r=1:3
figure;
px= vardata(:,250, r);
plot(px,'-', 'color',rand(1,3));
end
Now, I need to add legends to each plot as pixel 1, pixel 2, pixel 3 etc automaticlally.
I am used to add legends manually and have not worked on adding legends in a loop before.
Many Thanks

채택된 답변

Walter Roberson
Walter Roberson 2022년 11월 13일
for r=1:3
figure;
px= vardata(:,250, r);
plot(px,'-', 'color',rand(1,3), 'DisplayName', "pixel " + r);
legend show
end
However since you are only displaying one line per figure window, normally you would use a title() instead of legend()
  댓글 수: 1
Naif Alsalem
Naif Alsalem 2022년 11월 13일
Many thanks @Walter Roberson, for the answer and suggestion. Yes title for one-line plot is really helpful.
Thanks again.
Cheers,

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by