[MATLAB]突破图窗一图--多子图绘制(subplot)

———————————————————————————————

函数介绍

在这里插入图片描述

MATLAB官网描述:
https://ww2.mathworks.cn/help/matlab/ref/subplot.html?searchHighlight=subplot&s_tid=srchtitle_support_results_1_subplot

二子图

代码主体

clc;clear
%% 第一个子图窗口
subplot(1,2,1); % 创建
hold on
x1 = linspace(0,2,400);
y1 = sin(x1);
l1 =  plot(x1, y1,'Color','#191970','LineWidth',2);


%% 第二个子图窗口
subplot(1,2,2); % 创建
hold on
x2 = linspace(0,2,400);
y2 = cos(x2);
l2 =  plot(x2, y2,'Color','#191970','LineWidth',2);

结果演示

在这里插入图片描述

———————————————————————————————

三子图

代码主体

%% 第一个子图窗口
subplot(2,2,1); % 创建
hold on
x1 = linspace(0,2,400);
y1 = sin(x1);
l1 =  plot(x1, y1,'Color','#191970','LineWidth',2);


%% 第二个子图窗口
subplot(2,2,2); % 创建
hold on
x2 = linspace(0,2,400);
y2 = cos(x2);
l2 =  plot(x2, y2,'Color','#191970','LineWidth',2);


%% 第三个子图窗口
subplot(1,2,2); % 创建
hold on
x3 = linspace(0,2,400);
y3 = exp(x3);
l3 =  plot(x3, y3,'Color','#191970','LineWidth',2);

结果演示

在这里插入图片描述

———————————————————————————————

四子图

代码主体


clc;clear
%% 第一个子图窗口
subplot(2,2,1); % 创建
hold on
x1 = linspace(0,2,400);
y1 = sin(x1);
l1 =  plot(x1, y1,'Color','#191970','LineWidth',2);


%% 第二个子图窗口
subplot(2,2,2); % 创建
hold on
x2 = linspace(0,2,400);
y2 = cos(x2);
l2 =  plot(x2, y2,'Color','#191970','LineWidth',2);


%% 第三个子图窗口
subplot(2,2,3); % 创建
hold on
x3 = linspace(0,2,400);
y3 = exp(x3);
l3 =  plot(x3, y3,'Color','#191970','LineWidth',2);

%% 第四个子图窗口
subplot(2,2,4); % 创建
hold on
x4 = linspace(0,2,400);
y4 = log(x4);
l4 =  plot(x4, y4,'Color','#191970','LineWidth',2);

结果演示

在这里插入图片描述

———————————————————————————————

四子图–进化

代码主体


clc;clear
%% 第一个子图窗口
subplot(2,2,1); % 创建
hold on
x1 = linspace(0,2*pi,400);
y1 = sin(x1);
l1 =  plot(x1, y1,'Color','#191970','LineWidth',2);


%% 第二个子图窗口
subplot(4,2,2); % 创建
hold on
x2 = linspace(0,2*pi,400);
y2 = cos(x2);
l2 =  plot(x2, y2,'Color','#191970','LineWidth',2);


%% 第三个子图窗口
subplot(4,2,4); % 创建
hold on
x3 = linspace(0,2,400);
y3 = exp(x3);
l3 =  plot(x3, y3,'Color','#191970','LineWidth',2);

%% 第四个子图窗口
subplot(2,1,2); % 创建
hold on
x4 = linspace(0,2,400);
y4 = log(x4);
l4 =  plot(x4, y4,'Color','#191970','LineWidth',2);

结果演示

在这里插入图片描述

———————————————————————————————

下期预告

下期将介绍如何在一图窗内实现多子图同时动态绘制!
感兴趣的伙伴不要错过哦!
(原理:多张多子图的叠加)

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的例,展示了如何在 MATLAB 中使用 subplot() 函数绘制多个子图。 ```matlab % 创建数据 x = -pi:0.1:pi; y1 = sin(x); y2 = cos(x); y3 = tan(x); % 绘制第一个子图 subplot(2,2,1); plot(x,y1); title('sin(x)'); % 绘制第二个子图 subplot(2,2,2); plot(x,y2); title('cos(x)'); % 绘制第三个子图 subplot(2,2,3); plot(x,y3); title('tan(x)'); % 绘制第四个子图 subplot(2,2,4); plot(x,y1,'r',x,y2,'g--',x,y3,'b:'); title('sin(x), cos(x) and tan(x)'); legend('sin(x)','cos(x)','tan(x)'); ``` 在这个例中,我们创建了三个不同的数据集,分别是 sin(x)、cos(x) 和 tan(x)。我们使用 subplot() 函数来创建一个 2x2 的图形,并在其中绘制四个子图。 第一个 subplot() 函数调用指定了参数 2,2,1,这意味着我们正在绘制一个 2x2 的图形,并且正在绘制第一个子图。我们将 sin(x) 数据集绘制在这个子图中,并为它添加一个标题。 第二个 subplot() 函数调用指定了参数 2,2,2,这意味着我们正在绘制第二个子图。我们将 cos(x) 数据集绘制在这个子图中,并为它添加一个标题。 第三个 subplot() 函数调用指定了参数 2,2,3,这意味着我们正在绘制第三个子图。我们将 tan(x) 数据集绘制在这个子图中,并为它添加一个标题。 最后一个 subplot() 函数调用指定了参数 2,2,4,这意味着我们正在绘制最后一个子图。我们将 sin(x)、cos(x) 和 tan(x) 数据集都绘制在这个子图中,并使用 legend() 函数为数据集添加图例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值