matlabsubplot示例
时间: 2023-07-07 07:03:53 浏览: 58
以下是一个简单的 MATLAB subplot 示例:
```matlab
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
subplot(2,1,1); % 2 rows, 1 column, plot 1
plot(x, y1, 'r');
title('sin(x)');
subplot(2,1,2); % 2 rows, 1 column, plot 2
plot(x, y2, 'b');
title('cos(x)');
```
这个代码片段将在同一图像中显示两个子图,每个子图显示一个函数的图形。 subplot 函数的第一个参数是行数,第二个参数是列数,第三个参数是当前子图的索引。在上面的示例中,我们使用了两个子图,因此总共有两行和一列。第一个子图的索引是1,第二个子图的索引是2。每个子图都显示了一个不同的函数,使用不同的线颜色。每个子图都有自己的标题。
相关问题
Matlab subplot
subplot 函数用于在同一个窗口中创建多个子图。语法格式如下:
subplot(m,n,p)
其中,m 是行数,n 是列数,p 是要选择的子图编号。例如,subplot(2,2,1) 表示将图像分成两行两列,选择第一个子图进行绘制。
示例:
```
subplot(2,2,1)
plot(x,y1)
subplot(2,2,2)
plot(x,y2)
subplot(2,2,3)
plot(x,y3)
subplot(2,2,4)
plot(x,y4)
```
上面的代码将绘制 4 个子图,每个子图显示一条曲线,并将这 4 个子图组成 2x2 的网格布局。
matlab subplot 图例
在Matlab中,可以使用subplot函数创建多个子图,并且可以在每个子图中添加图例。下面是一个示例代码:
```matlab
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
subplot(2,1,1);
plot(x, y1, 'r', 'LineWidth', 2);
hold on;
plot(x, y2, 'b', 'LineWidth', 2);
legend('sin(x)', 'cos(x)');
title('Subplot 1');
subplot(2,1,2);
plot(x, y1, 'r', 'LineWidth', 2);
hold on;
plot(x, y2, 'b', 'LineWidth', 2);
legend('sin(x)', 'cos(x)', 'Location', 'southwest');
title('Subplot 2');
```
在这个例子中,我们使用subplot函数创建了一个2行1列的图形窗口,并在每个子图中绘制了sin(x)和cos(x)的曲线。在第一个子图中,我们使用默认的图例位置,而在第二个子图中,我们使用了'southwest'参数来设置图例的位置。
请注意,subplot函数的第一个参数是行数,第二个参数是列数,第三个参数是当前子图的索引。在每个子图中,我们使用plot函数绘制曲线,并使用legend函数添加图例。可以通过传递不同的位置参数来设置图例的位置。