在MATLAB画图中,有时需要忽略某些legend,有几种方法可以实现:
- 在画不需要图例的线条时添加
'HandleVisibility','off'
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1);
hold on
y2 = cos(2*x);
plot(x,y2,'HandleVisibility','off');
y3 = cos(3*x);
plot(x,y3);
hold off
legend('First','Third')
- 在
legend
函数中添加subset
,例如
x = linspace(0,pi);
y1 = cos(x);
p1 = plot(x,y1);
hold on
y2 = cos(2*x);
p2 = plot(x,y2);
y3 = cos(3*x);
p3 = plot(x,y3);
hold off
legend([p1 p3],{'First','Third'})
两者效果相同