Drawing a line without creating a legend entry

86 次查看(过去 30 天)
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;
h(2) = plot([1:3:10],'Color','b'); hold off;


更多回答(1 个)

Carlos Alberto Diaz Galindo
I think it's easier this way
hold on
h = legend('a','b')
h.AutoUpdate = 'off'
hold on


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