matlab subplot 图例
时间: 2024-01-07 09:22:21 浏览: 54
在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函数添加图例。可以通过传递不同的位置参数来设置图例的位置。
相关问题
matlab subplot加线
在Matlab中,使用subplot函数可以将多个图画到一个平面上。subplot函数的基本语法是subplot(m,n,p),其中m表示图排成m行,n表示图排成n列,p表示要将曲线画到figure中的哪个图上。例如,subplot(1,2,1)表示将曲线画到第一个图上,subplot(1,2,2)表示将曲线画到第二个图上。在subplot之后,可以使用plot函数来画线。例如,plot(x1,y1,'k-O')表示画一条黑色实线,带有圆圈标记的曲线。可以使用set函数来设置线的属性,例如线宽和点的大小。最后,可以使用title、xlabel、ylabel等函数来设置图的标题、x轴名称和y轴名称。在subplot中,可以使用grid on来打开网格线,使用legend来添加图例。
#### 引用[.reference_title]
- *1* [【转】matlab标题,给figure加标题,subplot的部分用法](https://blog.csdn.net/weixin_30972131/article/details/115851195)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [Matlab作图学习总结——plot,subplot](https://blog.csdn.net/dongke1991/article/details/104802760)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
matlab 子图 图例
Matlab中可以使用legend函数添加图例,使用subplot函数创建子图。下面是一个例子:
```matlab
% 创建数据
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
% 创建子图
subplot(1, 2, 1);
plot(x, y1, 'r');
legend('sin(x)');
subplot(1, 2, 2);
plot(x, y2, 'b');
legend('cos(x)');
```
这个例子创建了一个1行2列的子图,分别绘制了sin(x)和cos(x)的图像,并在每个子图中添加了相应的图例。其中,subplot(1, 2, 1)表示第一个子图,subplot(1, 2, 2)表示第二个子图。legend('sin(x)')和legend('cos(x)')分别添加了相应的图例。