Can't get the legend right

조회 수: 2 (최근 30일)
Salad Box
Salad Box 2022년 12월 7일
댓글: Voss 2022년 12월 8일
Hi
I plotted a few things on the chromaticity diagram below.
h1 to h3 are to plot three black lines, h4 is to plot dataset A, and h5 is to plot dataset B (with psudo code given below).
h1 = plot(line1)
h2 = plot(line2)
h3 = plot(line3)
h4 = plot(dataset A)
h5 = plot(dataset B)
When I put
legend
It shows the legend as below.
But I only need the legend for datasets A and B.
How to use the legend properly in this case?

채택된 답변

Voss
Voss 2022년 12월 7일
One way is to pass the graphics objects (i.e., lines, etc.) in as inputs to legend:
figure()
h1 = plot(1:10);
hold on
h2 = plot(2:11);
h3 = plot(3:12);
% legend for h2 and h3 only
legend([h2 h3],{'h2' 'h3'})
Another way would be to set the HandleVisibility of the objects you don't want in the lengend to 'off':
figure()
h1 = plot(1:10,'HandleVisibility','off');
hold on
h2 = plot(2:11);
h3 = plot(3:12);
% no need to supply [h2 h3] as the first argument this time
% (probably still a good idea though)
legend({'h2' 'h3'})
  댓글 수: 4
Salad Box
Salad Box 2022년 12월 7일
That's really useful to know. Thank you.
Voss
Voss 2022년 12월 8일
You're welcome!

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

추가 답변 (0개)

카테고리

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