t = linspace(0,1,30);
hold on, grid on
styles = {'b.-', 'r-o', 'g-o'};
for k = 1:12
plot(t, cos(pi/3*t+(k-1)/14*2*pi).*(1-t).^2, styles{mod(k-1,3)+1}, 'linewidth', .8)
end
然后,您要做的就是:
'HandleVisibility'通过将其属性设置为 来防止所有图出现在图例中'off';
使用所需的图例样式创建虚拟图表。这些图表包含NaN数值,因此它们不会显示在图表上。
set(get(gca, 'children'), 'HandleVisibility', 'off') % existing plots: not in legend
legend_styles = {'b.-', 'r-o', 'g-o'};
legend_texts = {'Line1', 'Line2', 'Line3'};
hold on % make sure existing plots are kept
for p = 1:numel(legend_styles)
plot(NaN, legend_styles{p}); % fake plot for legend
end
legend(legend_texts{:})
您可以使用图的
'HandleVisibility'
属性来实现这一点,该属性控制它们是否出现在图例中。由于您没有在代码中提供示例图(请下次提供),让我们创建一个:
然后,您要做的就是:
'HandleVisibility'
通过将其属性设置为 来防止所有图出现在图例中'off'
;NaN
数值,因此它们不会显示在图表上。