t = 0 : 0.01 : 2 * pi;
s = sin(t);
c = cos(t);
m = -sin(t);
hold on;
plot(t, s, \'r\');
plot(t, c, \'b\');
plot(t, m, \'g\');
hold off;
legend(\'\', \'cosine\', \'
Let's start with your variables and plot them:
t = 0 : 0.01 : 2 * pi;
s = sin(t);
c = cos(t);
m = -sin(t);
figure;
hold ('all');
hs = plot(t, s);
hc = plot(t, c);
hm = plot(t, m);
There is a property called IconDisplayStyle. It is buried quite deep. The path you need to follow is:
Line -> Annotation -> LegendInformation -> IconDisplayStyle
Setting the IconDisplayStyle
property off
will let you skip that line. As an example, I am going to turn off hs
's legend.
hsAnno = get(hs, 'Annotation');
hsLegend = get(hsAnno, 'LegendInformation');
set(hsLegend, 'IconDisplayStyle', 'off');
Of course you can go ahead and do it like this:
set(get(get(hs, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off');
But I find it much harder to understand.
Now, the legend
function will just skip hs
.
Ending my code with this:
legend('cosine', 'repeat for this handle')
will give you this:
EDIT: Jonas had a nice suggestion in the comments:
Setting the DisplayName
property of hc like this:
set(hc, 'DisplayName', 'cosine');
legend(gca, 'show');
will give you the legend you need. You will have associated your line handle with 'cosine'
. So, you can just call the legend with 'off'
or 'show'
parameters.