MATLAB hold 控制是否在同一图形上叠加曲线。默认 plot 会清掉旧图,hold on 后可继续追加。
语法
hold on % 保留当前图,继续画
hold off % 恢复默认:新 plot 覆盖旧图示例
x = 0:0.01:10;
plot(x, sin(x), 'r')
hold on
plot(x, cos(x), 'b--')
hold off
legend('sin', 'cos')不用 hold 的等价写法(一次多曲线):
plot(x, sin(x), x, cos(x))注意事项
- 画完记得
hold off,否则后面所有图都会糊在一起; - legend 按绘制顺序对应,hold 分步画时顺序即调用顺序。