Main Content

subplot

在各个分块位置创建坐标区

说明

示例

注意

subplot 相比,推荐使用 tiledlayout,因为它使您能够创建具有可调图块间距、根据图窗大小重排的图块以及放置更合理的颜色栏和图例的布局。 (自 R2019b 起)

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

示例

subplot(m,n,p,'replace') 删除位置 p 处的现有坐标区并创建新坐标区。

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

示例

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

示例

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

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

示例

ax = subplot(___) 创建一个 Axes 对象、PolarAxes 对象或 GeographicAxes 对象。以后可以使用 ax 修改坐标区。

示例

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

示例

全部折叠

创建带有两个堆叠子图的图窗。在每个子图上绘制一条正弦波。

subplot(2,1,1);
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)

subplot(2,1,2); 
y2 = sin(5*x);
plot(x,y2)

Figure contains 2 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line.

创建一个分为四个子图的图窗。在每个子图上绘制一条正弦波并为每个子图指定标题。

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)')

Figure contains 4 axes objects. Axes object 1 with title Subplot 1: sin(x) contains an object of type line. Axes object 2 with title Subplot 2: sin(2x) contains an object of type line. Axes object 3 with title Subplot 3: sin(4x) contains an object of type line. Axes object 4 with title Subplot 4: sin(8x) contains an object of type line.

创建一个包含三个子图的图窗。在图窗的上半部分创建两个子图,并在图窗的下半部分创建第三个子图。在每个子图上添加标题。

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')

Figure contains 3 axes objects. Axes object 1 with title Subplot 1: Cosine contains an object of type line. Axes object 2 with title Subplot 2: Polynomial contains an object of type line. Axes object 3 with title Subplot 3 and 4: Both contains 2 objects of type line.

创建一个包含四个随机数据针状图的图窗。然后将第二个子图替换为空坐标区。

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

Figure contains 4 axes objects. Axes object 1 contains an object of type stem. Axes object 2 contains an object of type stem. Axes object 3 contains an object of type stem. Axes object 4 contains an object of type stem.

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

Figure contains 4 axes objects. Axes object 1 contains an object of type stem. Axes object 2 contains an object of type stem. Axes object 3 contains an object of type stem. Axes object 4 is empty.

创建包含两个未与网格位置对齐的子图的图窗。为每个子图指定一个自定义位置。

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')

Figure contains 2 axes objects. Axes object 1 with title First Subplot contains 4 objects of type line. Axes object 2 with title Second Subplot contains 4 objects of type bar.

创建包含两个极坐标区的图窗。在上部子图中创建极坐标线图,在下部子图中创建极坐标散点图。

figure
ax1 = subplot(2,1,1,polaraxes);
theta = linspace(0,2*pi,50);
rho = sin(theta).*cos(theta);
polarplot(ax1,theta,rho)

ax2 = subplot(2,1,2,polaraxes);
polarscatter(ax2,theta,rho)

Figure contains 2 axes objects. Polaraxes object 1 contains an object of type line. Polaraxes object 2 contains an object of type scatter.

创建带有两个子图的图窗。将 Axes 对象赋给变量 ax1ax2。将 Axes 对象指定为绘图函数的输入,以确保绘图函数在特定的子图中进行绘制。

ax1 = subplot(2,1,1);
Z = peaks;
plot(ax1,Z(1:20,:))

ax2 = subplot(2,1,2);  
plot(ax2,Z)

Figure contains 2 axes objects. Axes object 1 contains 49 objects of type line. Axes object 2 contains 49 objects of type line.

通过设置 Axes 对象的属性来修改坐标区。更改上部子图的字体大小和下部子图的线宽。某些绘图函数可设置坐标区属性。先执行绘图函数,然后指定坐标区属性以免覆盖现有坐标区属性的设置。使用圆点表示法设置属性。

ax1.FontSize = 15;
ax2.LineWidth = 2;

Figure contains 2 axes objects. Axes object 1 contains 49 objects of type line. Axes object 2 contains 49 objects of type line.

创建一个包含多个子图的图窗。将 Axes 对象存储在向量 ax 中。然后使第二个子图成为当前坐标区。创建一个线图并更改第二个子图的坐标轴范围。默认情况下,图形函数作用于当前坐标区。

for k = 1:4
    ax(k) = subplot(2,2,k);
end

subplot(ax(2))
x = linspace(1,50);
y = sin(x);
plot(x,y,'Color',[0.1, 0.5, 0.1])
title('Second Subplot')
axis([0 50 -1 1])

Figure contains 4 axes objects. Axes object 1 is empty. Axes object 2 with title Second Subplot contains an object of type line. Axes object 3 is empty. Axes object 4 is empty.

创建一个线图。然后转换坐标区,使其成为图窗的下部子图。subplot 函数使用原始坐标区所在的图窗。

x = linspace(1,10);
y = sin(x);
plot(x,y)
title('Sine Plot')

Figure contains an axes object. The axes object with title Sine Plot contains an object of type line.

ax = gca;
subplot(2,1,2,ax)

Figure contains an axes object. The axes object with title Sine Plot contains an object of type line.

将位于不同图窗中的坐标区合并到包含子图的单个图窗中。

在两个不同的图窗中创建两个图。将 Axes 对象赋给变量 ax1ax2。将 Legend 对象赋给变量 lgd

figure
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('Line Plot 1')
ax1 = gca;

Plot of a sine wave entitled "Line Plot 1"

figure
y2 = 2*sin(x);
plot(x,y2)
title('Line Plot 2')
lgd = legend('2*Sin(x)');
ax2 = gca;

Plot of a sine wave with a legend entitled "Line Plot 2"

使用 copyobj 创建两个 Axes 对象的副本。将复制的坐标区的父级指定为新图窗。由于图例和颜色栏不会随相关坐标区一起复制,因此请随坐标区一起复制图例。

fnew = figure;
ax1_copy = copyobj(ax1,fnew);
subplot(2,1,1,ax1_copy)

copies = copyobj([ax2,lgd],fnew);
ax2_copy = copies(1);
subplot(2,1,2,ax2_copy)

Figure containing both of the preceding plots

输入参数

全部折叠

网格行数,指定为正整数。

数据类型: single | double

网格列数,指定为正整数。

数据类型: single | double

新坐标区的网格位置,指定为标量或正整数向量。

  • 如果 p 是正整数标量,则 subplot 在网格位置 p 处创建一个子图。

  • 如果 p 是正整数向量,则 subplot 新建一个跨 p 中列出的网格位置的子图。

示例: subplot(2,3,1) 在位置 1 处创建一个子图。

示例: subplot(2,3,[2,5]) 创建跨位置 2 和 5 的子图。

示例: subplot(2,3,[2,6]) 创建跨位置 2、3、5 和 6 的子图。

数据类型: single | double

新坐标区的自定义位置,指定为 [left bottom width height] 形式的四元素向量。

  • leftbottom 元素指定子图的左下角相对于图窗的左下角的位置。

  • widthheight 元素指定子图维度。

指定介于 01 之间的归一化值(基于图窗内界)。

注意

使用脚本创建子图时,在发出 drawnow 命令或 MATLAB 返回到等待用户命令之前,MATLAB 不会最终确定 Position 属性值。在脚本刷新绘图或退出之前,子图的 Position 属性值会受到变化的影响。

示例: subplot('Position',[0.1 0.1 0.45 0.45])

数据类型: single | double

要设为当前坐标区或转换为子图的现有坐标区,指定为 Axes 对象、PolarAxes 对象、GeographicAxes 对象或具有 PositionConstraint 属性的图形对象,如 HeatmapChart 对象。

要在子图位置创建空的极坐标区或地理坐标区,请将 ax 指定为 polaraxesgeoaxes 函数。例如,subplot(2,1,2,polaraxes)

名称-值参数

将可选的参数对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参数名称,Value 是对应的值。名称-值参数必须出现在其他参数之后,但参数对组的顺序无关紧要。

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: subplot(m,n,p,'XGrid','on')

某些绘图函数会覆盖属性设置。请考虑在绘图后设置坐标区属性。您可以设置的属性取决于坐标区的类型:

提示

  • 要清除图窗的内容,请使用 clf。例如,您可以在创建新子图布局之前清除图窗中的现有子图布局。

  • 要叠加坐标区,请改用 axes 命令。subplot 函数会删除与新坐标区重叠的现有坐标区。例如,subplot('Position',[.35 .35 .3 .3]) 会删除所有底层坐标区,但 axes('Position',[.35 .35 .3 .3]) 会将新坐标区置于图窗的中部而不删除底层坐标区。

  • subplot(111) 是一个例外,其行为与 subplot(1,1,1) 不同。出于向后兼容的原因,subplot(111) 是子图的一种特殊情况,它不立即创建坐标区,而是设置图窗,以便接下来的图形命令执行 clf reset。接下来的图形命令将删除所有图窗子级,并在默认位置创建新的坐标区。subplot(111) 不返回 Axes 对象,如果代码指定了返回参数,将会发生错误。

替代功能

使用 tiledlayoutnexttile 函数创建可配置的绘图平铺。配置选项包括:

  • 对绘图之间和布局边缘周围间距的控制

  • 布局顶部共享标题的选项

  • 共享 x 轴和 y 轴标签的选项

  • 用于控制分块是固定大小还是可以调整的可变大小的选项

有关详细信息,请参阅合并多个绘图

版本历史记录

在 R2006a 之前推出