matlab如何用plot做两张图
时间: 2024-10-18 08:17:16 浏览: 92
在MATLAB中,如果你想在同一图形窗口内绘制两张图,可以使用`subplot`函数或者直接绘制两行两列的子图。以下是两种常见方法:
**方法一:使用subplot()函数**
```matlab
% 创建一个2x2的网格
figure
subplot(2, 1, 1) % 第一个子图,从左到右,从上到下
% 绘制第一张图的代码,如 y = sin(x)
x = linspace(0, 2*pi, 100);
y1 = sin(x);
plot(x, y1)
subplot(2, 1, 2) % 第二个子图
% 绘制第二张图的代码,如 y = cos(x)
y2 = cos(x);
plot(x, y2)
```
这个例子中,第一行的第一个子图会显示正弦波形,第二行的第二个子图会显示余弦波形。
**方法二:绘制两行两列子图**
```matlab
figure
hold on % 保持当前绘图模式,以便在同一图中添加更多线条
% 绘制第一幅图
plot(x, y1, 'r') % 使用红色绘制sin(x)
% 绘制第二幅图,这里不需要指定x轴,因为已经在第一次绘图中设置了
plot(y2, 'g') % 使用绿色绘制cos(x)
% 如果你想在新的一行继续添加图形,可以先clear plot
clear;
% 然后再绘制第三幅图(这里省略了x轴)
plot(z1, 'b') % 蓝色绘制第三个图形z1
% 最后关闭hold模式,防止后续绘制覆盖前两幅图
hold off
```
这种方法更适合于只需要简单地堆叠几个独立的图形。
**相关问题--:**
1. 在MATLAB中如何同时显示多条曲线在一个子图里?
2. subplot()函数中参数是什么意思?
3. hold on 和 hold off命令分别做什么?
阅读全文
相关推荐


















