让我们从变量开始并绘制它们:
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);
有一个名为IconDisplayStyle 的属性。它埋得很深。您需要遵循的路径是:
行 -> 注释 -> LegendInformation -> IconDisplayStyle
设置IconDisplayStyle 属性off 将让您跳过该行。例如,我将关闭hs的图例。
hsAnno = get(hs, 'Annotation');
hsLegend = get(hsAnno, 'LegendInformation');
set(hsLegend, 'IconDisplayStyle', 'off');
当然你可以继续这样做:
set(get(get(hs, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off');
但我发现它更难理解。
现在,legend 函数将跳过 hs。
以此结束我的代码:
legend('cosine', 'repeat for this handle')
会给你这个:
编辑:乔纳斯在 cmets 中有一个很好的建议:
像这样设置 hc 的DisplayName 属性:
set(hc, 'DisplayName', 'cosine');
legend(gca, 'show');
将为您提供所需的图例。您将把您的线路句柄与'cosine' 关联起来。因此,您可以使用'off' 或'show' 参数调用图例。