主要内容

newplot

R2026b

指定图形对象的绘制位置

描述

newplot 通过确保将新绘图添加到或替换现有绘图,为新绘图准备当前坐标区。newplot 的行为取决于坐标区及其父图窗的 NextPlot 属性。有关详细信息,请参阅算法。

如果不存在坐标区,newplot 会在当前图窗中创建一个新的笛卡尔坐标区对象。

对于简单二维图,请改用 plot 函数。

示例

newplot(ax) 更新指定坐标区 ax 而不是当前坐标区。您可以在图形代码的开头使用此 newplot 语法来确定对哪些坐标区和图窗使用后续绘图命令。

示例

cax = newplot(___) 返回 Axes 对象。使用上述任一语法指定输出参量。在创建后,使用 cax 查询和修改坐标区属性。有关属性列表,请参阅 Axes 属性。

示例

示例

全部折叠

创建默认绘图坐标区。

newplot

Figure contains a Cartesian axes object.

在图窗中放置两个 Axes 对象,并为每个 Axes 对象添加一个绘图。将坐标区指定为每个绘图函数的第一个输入参量。

tiledlayout('flow')
ax1 = nexttile;
ax2 = nexttile;

plot(ax1,peaks(20))
surf(ax2,peaks(20))

view([450 0])

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

默认情况下,ax2 的 NextPlot 属性值为 'replace'。将 NextPlot 属性值更改为 'add'。

ax2.NextPlot
ans = 
'replace'
ax2.NextPlot = 'add';

当坐标区 NextPlot 属性值为 'add' 时,在坐标区中绘图不会重置坐标区属性或从坐标区中删除子对象。新值与现有子对象一起绘制。

newplot(ax2)
surf(ax2,peaks(5)+10)

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

创建两个 Axes 对象,并向其中一个对象添加绘图。

tiledlayout("flow")
ax1 = nexttile;
ax2 = nexttile;

surf(ax1,peaks(20))

nexttile(1)
view([450 0])

Figure contains 2 axes objects. Axes object 1 contains an object of type surface. Axes object 2 is empty.

返回 Axes 对象,并通过设置该对象的属性来自定义坐标区的外观。例如,显示框轮廓和网格线。然后将坐标区的 NextPlot 属性设置为 "add"。

curr = newplot(ax2);

curr.Box = "on";
curr.XGrid = "on";
curr.YGrid = "on";
curr.NextPlot = "add";

向第二个 Axes 对象添加绘图。

plot(curr,peaks(20))

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

编写名为 myPlot 的函数,该函数在绘制数据的同时考虑到了保留状态,无论该状态是 on 还是 off。在该函数中,调用 newplot 为新绘图准备当前坐标区;如果不存在坐标区,则创建坐标区。

function myPlot(data)
ax = newplot;
line(ax,XData=1:numel(data),YData=data,Marker="o");
end

使用 myPlot 绘制一些数据。如果找不到现有坐标区,则在 myPlot 函数中调用 newplot 会创建新坐标区。

d = linspace(0,12,100);
myPlot(sin(d));

打开 hold 状态,并使用 myPlot 绘制另一组数据。由于 hold 函数设置当前坐标区的 NextPlot 属性,因此对 newplot 的本次调用可确保 myPlot 函数会将该数据添加到现有绘图中。

hold on
myPlot(cos(d));
hold off

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

现在,便可用新绘图替换坐标区的内容。由于 hold 已关闭,因此 myPlot 会替换以前坐标区的内容。

myPlot(cos(d+pi));

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

输入参数

全部折叠

要更改或替换的坐标区,指定为 Axes、GeographicAxes 或 PolarAxes 对象。

算法

newplot 函数的行为取决于图窗的 NextPlot 属性和坐标区的 NextPlot 属性。

首先,newplot 函数会查询图窗的 NextPlot 属性。下表显示 newplot 函数如何根据 NextPlot 的值更改图窗。

NextPlot 属性的值(图窗)

newplot 的行为

"add"(默认值)

  • 不从图窗中删除子对象。

  • 不要重置图窗属性。

"new"

  • 创建一个新图窗。

"replacechildren"

  • 从图窗中删除所有未隐藏的子对象(即,其 HandleVisibility 属性设置为 "on" 的子对象)。

  • 将图窗的 NextPlot 属性重置为 "add"。

此行为等效于使用 clf 函数。

"replace"

  • 从图窗中删除所有子对象,即使子对象处于隐藏状态也是如此。

  • 将所有图窗属性重置为其默认值,但 Position、Units、PaperPosition 和 PaperUnits 除外。

  • 将图窗的 NextPlot 属性重置为 "add",不考虑用户定义的默认值。

此行为等效于使用 clf 函数的 clf reset 语法。

然后,newplot 函数查询图窗中坐标区的 NextPlot 属性。下表显示 newplot 函数如何根据其 NextPlot 属性的值来更改坐标区。

NextPlot 属性的值(坐标区)

newplot 的行为

"replace"(默认值)

  • 从坐标区中删除所有子对象。

  • 将所有坐标区属性重置为其默认值,但 Position 和 Units 除外。

此行为等效于调用带 reset 参量的 cla 函数。

"add"

  • 不从坐标区中删除子对象。

  • 不要重置坐标区属性。

"replacechildren"

  • 从坐标区中删除所有子对象。

  • 将坐标区的 NextSeriesIndex 属性重置为 1。

此行为等效于调用不带 reset 参量的 cla 函数。

"replaceall"

  • 从坐标区中删除所有子对象。

  • 将所有坐标区属性重置为其默认值,但 Position 和 Units 除外。

对于只有一个 y 轴的坐标区,"replace" 和 "replaceall" 值是等效的。对于有两个 y 轴的坐标区,"replace" 值只影响活动侧,而 "replaceall" 值同时影响两侧。

版本历史记录

在 R2006a 之前推出