Main Content

合并多个绘图

自 R2019b 起. 替换 Combine Multiple Plots (R2019a).

以下示例说明了如何使用 hold 函数合并同一坐标区中的绘图,以及如何使用 tiledlayout 函数在图窗中创建多个坐标区。

在同一坐标区中合并绘图

默认情况下,新图将清除现有图,并重置标题等坐标区属性。但是,您可以使用 hold on 命令在同一坐标区中合并多个图。例如,绘制两条直线和一个散点图,然后将 hold 状态重置为 off。

x = linspace(0,10,50);
y1 = sin(x);
plot(x,y1)
title('Combine Plots')

hold on

y2 = sin(x/2);
plot(x,y2)

y3 = 2*sin(x);
scatter(x,y3) 

hold off

Figure contains an axes object. The axes object with title Combine Plots contains 3 objects of type line, scatter.

启用 hold 状态后,新图不会清除现有图,也不会重置标题或轴标签等坐标区属性。新图将根据坐标区的 ColorOrderLineStyleOrder 属性循环使用颜色和线型。坐标区范围和刻度值可能会进行调整以适应新数据。

在图窗中显示多个坐标区

您可以使用 tiledlayout 函数在单个图窗中显示多个坐标区。此函数将创建一个分块图布局,该布局将图窗分为一系列不可见的图块网格。每个图块可以包含一个用于显示绘图的坐标区。创建布局后,调用 nexttile 函数将坐标区对象放置到布局中。然后调用绘图函数在该坐标区中绘图。例如,在一个 2×1 布局中创建两个绘图。为每个绘图添加标题。

x = linspace(0,10,50);
y1 = sin(x);
y2 = rand(50,1);
tiledlayout(2,1)

% Top plot
nexttile
plot(x,y1)
title('Plot 1')

% Bottom plot
nexttile
scatter(x,y2)
title('Plot 2')

Figure contains 2 axes objects. Axes object 1 with title Plot 1 contains an object of type line. Axes object 2 with title Plot 2 contains an object of type scatter.

创建跨多行或多列的绘图

要创建跨多行或多列的绘图,请在调用 nexttile 时指定 span 参量。例如,创建一个 2×2 布局。绘制前两个图块。然后创建一个跨一行两列的图。

x = linspace(0,10,50);
y1 = sin(x);
y2 = rand(50,1);

% Top two plots
tiledlayout(2,2)
nexttile
plot(x,y1)
nexttile
scatter(x,y2)

% Plot that spans
nexttile([1 2])
y2 = rand(50,1);
plot(x,y2)

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

修改坐标区外观

通过在每个坐标区对象上设置属性来修改坐标区外观。您可以通过带输出参量调用 nexttile 函数来获取坐标区对象。您也可以将坐标区对象指定为图形函数的第一个输入参量,以确保该函数作用于正确的坐标区。

例如,创建两个绘图,并将坐标区对象赋给变量 ax1ax2。更改第一个绘图的坐标区字体大小和 x 轴颜色。为第二个绘图添加网格线。

x = linspace(0,10,50);
y1 = sin(x);
y2 = rand(50,1);
tiledlayout(2,1)

% Top plot
ax1 = nexttile;
plot(ax1,x,y1)
title(ax1,'Plot 1')
ax1.FontSize = 14;
ax1.XColor = 'red';

% Bottom plot
ax2 = nexttile;
scatter(ax2,x,y2)
title(ax2,'Plot 2')
grid(ax2,'on')

Figure contains 2 axes objects. Axes object 1 with title Plot 1 contains an object of type line. Axes object 2 with title Plot 2 contains an object of type scatter.

控制图块周围的间距

您可以通过指定 PaddingTileSpacing 属性来控制布局中图块周围的间距。例如,在一个 2×2 布局中显示四个绘图。

x = linspace(0,30);
y1 = sin(x);
y2 = sin(x/2);
y3 = sin(x/3);
y4 = sin(x/4);

% Create plots
t = tiledlayout(2,2);
nexttile
plot(x,y1)
nexttile
plot(x,y2)
nexttile
plot(x,y3)
nexttile
plot(x,y4)

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

通过将 PaddingTileSpacing 属性设置为 'compact',减小布局四周和每个图块周围的间距。

t.Padding = 'compact';
t.TileSpacing = 'compact';

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

显示共享标题和轴标签

您可以在布局中显示共享标题和共享轴标签。创建一个 2×1 布局 t。然后显示一个线图和一个针状图。通过调用 linkaxes 函数来同步 x 轴范围。

x1 = linspace(0,20,100);
y1 = sin(x1);
x2 = 3:17;
y2 = rand(1,15);

% Create plots.
t = tiledlayout(2,1);
ax1 = nexttile;
plot(ax1,x1,y1)
ax2 = nexttile;
stem(ax2,x2,y2)

% Link the axes
linkaxes([ax1,ax2],'x');

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

通过将 t 传递给 titlexlabelylabel 函数,添加共享标题和共享轴标签。通过从顶部图中删除 x 轴刻度标签,并将 tTileSpacing 属性设置为 'compact',让图彼此更靠近。

% Add shared title and axis labels
title(t,'My Title')
xlabel(t,'x-values')
ylabel(t,'y-values')

% Move plots closer together
xticklabels(ax1,{})
t.TileSpacing = 'compact';

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

另请参阅

函数

相关主题