Drawing a line without creating a legend entry

조회 수: 136 (최근 30일)
Leo Simon
Leo Simon 2020년 8월 14일
편집: Walter Roberson 2024년 3월 13일
In the MWE below, the green line shows up as the second row of the legend box. I want to be able to draw lines and incrementally build the legend as illustrated, but without having the line enter into the legend as it does in this example. Obviously, if I do all the plots before drawing the lines, and then use AutoUpdate off, I can accomplish this, but sometimes I need to intersperse lines and plots as in the example.
Is there any way to do this?
close all ; hold on;plot(1:10);Legend = {'line 1'};h=line([1,10],[6,6],'Color','g');hold on;plot(10:-1:1);Legend=[Legend,'line 2'];legend(Legend)

채택된 답변

Walter Roberson
Walter Roberson 2020년 8월 14일
You can set the IconDisplayStyle of the Annotation property in order to exclude a graphics object from automatic legend.
  댓글 수: 1
Leo Simon
Leo Simon 2020년 8월 14일
Thanks very much for the referral, Walter. @Jon's answer is incredibly simple and does what I want. Here's an alternative that doesn't require DisplayName
h(1) = plot([1:10],'Color','r');hold on;
line([1,10],[6,6],'Color','g');
h(2) = plot([1:3:10],'Color','b'); hold off;
Legend={'a','b'};
legend(h([1,2]),Legend);

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

추가 답변 (1개)

Carlos Alberto Diaz Galindo
Carlos Alberto Diaz Galindo 2023년 2월 16일
편집: Walter Roberson 2024년 3월 13일
I think it's easier this way
plot(x1,y1)
hold on
plot(x2,y2)
h = legend('a','b')
h.AutoUpdate = 'off'
hold on
plot(x3,y3)

카테고리

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