Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

hold

添加新绘图时保留当前绘图

说明

示例

hold on 保留当前坐标区中的绘图,从而使新添加到坐标区中的绘图不会删除现有绘图。新绘图基于坐标区的 ColorOrderLineStyleOrder 属性使用后续的颜色和线型。MATLAB® 将调整坐标区的范围、刻度线和刻度标签以显示完整范围的数据。如果不存在坐标区,hold 命令会创建坐标区。

示例

hold off 将保留状态设置为 off,从而使新添加到坐标区中的绘图清除现有绘图并重置所有的坐标区属性。添加到坐标区的下一个绘图基于坐标区的 ColorOrderLineStyleOrder 属性使用第一个颜色和线型。此选项为默认行为。

hold allhold on 相同。在以后的版本中将会删除该语法。请改用 hold on

hold 在 on 和 off 之间切换保留状态。

示例

hold(ax,___)ax 指定的坐标区而非当前坐标区设置 hold 状态。请在前面任何语法中的所有其他参数之前指定 ax。使用单引号将 'on''off' 输入引起来,例如 hold(ax,'on')

示例

全部折叠

创建一个线图。使用 hold on 添加第二个线图,而不删除已有的线图。新绘图基于坐标区的 ColorOrderLineStyleOrder 属性使用下一种颜色和线型。然后将 hold 状态重置为 off。

x = linspace(-pi,pi);
y1 = sin(x);
plot(x,y1)

hold on
y2 = cos(x);
plot(x,y2)
hold off

Figure contains an axes object. The axes object contains 2 objects of type line.

当 hold 状态为 off 时,新绘图将删除现有绘图。新绘图从色序和线型序列的开头开始。

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

Figure contains an axes object. The axes object contains an object of type line.

从 R2019b 开始,您可以使用 tiledlayoutnexttile 函数显示分块图。调用 tiledlayout 函数以创建一个 2×1 分块图布局。调用 nexttile 函数以创建坐标区对象 ax1ax2。在每个坐标区中绘制一个正弦波图。

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

% Top plot
ax1 = nexttile;
plot(ax1,x,y1)

% Bottom plot
ax2 = nexttile;
plot(ax2,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.

向上方坐标区添加第二个正弦波。

hold(ax1,'on')
y3 = sin(2*x);
plot(ax1,x,y3)
hold(ax1,'off')

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

创建 1×2 分块图布局。调用 nexttile 函数以创建两个坐标区对象并在坐标区中绘图。

t = tiledlayout(1,2);
ax1 = nexttile;
ax2 = nexttile;
plot(ax1,[0 1 0 1])
scatter(ax2,rand(1,10),rand(1,10),'filled')

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

将两个坐标区的 hold 状态都设置为 'on'。然后在每个图中显示附加数据。

hold([ax1 ax2],'on')
plot(ax1,[.5 .2 .5 .2])
scatter(ax2,rand(1,10),rand(1,10),'filled')

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

输入参数

全部折叠

目标坐标区,指定为下列值之一:

  • 任何类型的坐标区对象:AxesPolarAxesGeographicAxes 对象。

  • 属于同一类的坐标区对象组成的数组。要确定类,请使用 class 函数。

如果您不指定坐标区,hold 将为当前坐标区设置 hold 状态。

提示

  • 使用 ishold 函数测试保留状态。

算法

hold 函数设置以下属性:

  • NextPlot 坐标区属性 - 将相关联的 AxesPolarAxesGeographicAxes 对象的此属性设置为 'add''replace'

  • NextPlot 图窗属性 - 将 Figure 对象的此属性设置为 'add'

版本历史记录

在 R2006a 之前推出