matlab绘图 subplot函数使用方法

在平铺位置创建坐标轴

语法
①subplot(m,n,p)
subplot(m,n,p) 将当前图形划分为 m×n 网格,并在 p 指定的位置创建坐标轴。MATLAB® 按行号对子图位置进行编号。第一个子图是第一行的第一列,第二个子图是第一行的第二列,依此类推。如果指定的位置已存在坐标轴,则此命令会将该坐标轴设为当前坐标轴。

②subplot(m,n,p,‘replace’)
subplot(m,n,p,‘replace’) 删除位置 p 处的现有坐标轴并创建新坐标轴。

③subplot(m,n,p,‘align’)
subplot(m,n,p,‘align’) 创建新坐标轴,以便对齐图框。此选项为默认行为。

④subplot(m,n,p,ax)
subplot(m,n,p,ax) 将现有坐标轴 ax 转换为同一图形中的子图。

⑤subplot(‘Position’,pos)
subplot(‘Position’,pos) 在 pos 指定的自定义位置创建坐标轴。使用此选项可定位未与网格位置对齐的子图。指定 pos 作为 [left bottom width height] 形式的四元素矢量。如果新坐标轴与现有坐标轴重叠,新坐标轴将替换现有坐标轴。

⑥subplot(___,Name,Value)
subplot(___,Name,Value) 使用一个或多个名称-值对组参数修改坐标轴属性。有关属性列表,请参阅 Axes 属性。在所有其他输入参数之后设置坐标轴属性。

⑦ax = subplot(___)
ax = subplot(___) 返回创建的 Axes 对象。以后可以使用 ax 修改坐标轴。有关属性列表,请参阅 Axes 属性。

⑧subplot(ax)
subplot(ax) 将 ax 指定的坐标轴设为父图的当前坐标轴。如果父图尚不是当前图形,此选项不会使父图成为当前图形。

示例

(1)上部和下部子图
创建带有两个堆叠子图的图形。在每个子图上绘制一条正弦波。

 subplot(2,1,1);
 x = linspace(0,10);
 y1 = sin(x);
 plot(x,y1)
 title('Subplot 1: sin(x)')
 
 subplot(2,1,2);
 y1 = sin(5*x);
 plot(x,y2)
 title('Subplot 2: sin(5x)')

在这里插入图片描述
(2)子图的象限
创建一个分为四个子图的图形。在每个子图上绘制一条正弦波并为每个子图指定标题。

情况一:图形编号在各个子图的上方

subplot(2,2,1)
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('Subplot 1: sin(x)')

subplot(2,2,2)
y2 = sin(2*x);
plot(x,y2)
title('Subplot 2: sin(2x)')

subplot(2,2,3)
y3 = sin(4*x);
plot(x,y3)
title('Subplot 3: sin(4x)')

subplot(2,2,4)
y4 = sin(8*x);
plot(x,y4)
title('Subplot 4: sin(8x)')

在这里插入图片描述

情况二:图形编号在各个子图的下方

subplot(2,2,1)
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('(a) sin(x)', 'Units', 'normalized', 'Position', [0.5, -0.3, 0])

subplot(2,2,2)
y2 = sin(2*x);
plot(x,y2)
title('(b) sin(2x)', 'Units', 'normalized', 'Position', [0.5, -0.3, 0])

subplot(2,2,3)
y3 = sin(4*x);
plot(x,y3)
title('(c) sin(4x)', 'Units', 'normalized', 'Position', [0.5, -0.3, 0])

subplot(2,2,4)
y4 = sin(8*x);
plot(x,y4)
title('(d) sin(8x)', 'Units', 'normalized', 'Position', [0.5, -0.3, 0]')

在这里插入图片描述

(3)大小不同的子图
创建一个包含三个子图的图形。在图形的上半部分创建两个子图,并在图形的下半部分创建第三个子图。在每个子图上添加标题。

subplot(2,2,1);
x = linspace(-3.8,3.8);
y_cos = cos(x);
plot(x,y_cos);
title('Subplot 1: Cosine')

subplot(2,2,2);
y_poly = 1 - x.^2./2 + x.^4./24;
plot(x,y_poly,'g');
title('Subplot 2: Polynomial')

subplot(2,2,[3,4]);
plot(x,y_cos,'b',x,y_poly,'g');
title('Subplot 3 and 4: Both')

在这里插入图片描述
(4)将子图替换为空坐标轴
创建一个包含四个随机数据针状图的图形。然后将第二个子图替换为空坐标轴

for k = 1:4
    data = rand(1,10);
    subplot(2,2,k)
    stem(data)
end

subplot(2,2,2,'replace')

在这里插入图片描述在这里插入图片描述
(5)自定义位置上的子图
创建包含两个未与网格位置对齐的子图的图形。为每个子图指定一个自定义位置。

pos1 = [0.1 0.3 0.3 0.3];
subplot('Position',pos1)
y = magic(4);
plot(y)
title('First Subplot')

pos2 = [0.5 0.15 0.4 0.7];
subplot('Position',pos2)
bar(y)
title('Second Subplot')

在这里插入图片描述

  • 180
    点赞
  • 1146
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值