MATLAB 术语:为什么使用 Parent 作为 Axis?

MATLAB Terminology: Why use Parent for Axis?

这是一个关于 Matlab Parent 应用程序的 MATLAB 术语问题。我经常在绘图中使用 axes1 = axes(‘Parent’, figure1),因为我已经记住了绘图步骤。但我什至不明白我为什么使用这条线。

我知道我们在 Matlab 图形中有父对象和子对象。但是我认为父对象只与图形有关,所有其他图形对象都在下面。 Parent 是什么意思我们申请了:axes1 = axes(‘Parent’, figure1).

Parentaxes对象或MATLAB中许多其他图形对象的属性,它存储对象父对象的句柄。

您用来创建 axes 对象的语法是 axes():

的重载

axes(Name,Value) modifies the axes appearance or controls the way data displays using one or more name-value pair arguments. For example, 'FontSize',14 sets the font size for the axes text.

参考:https://uk.mathworks.com/help/matlab/ref/axes.html?s_tid=doc_ta

因此,axes1 = axes('Parent', figure1) 创建一个 axes 对象并将 figure1 分配给它的 Parent 属性。通过这样做,axes 被放置在 figure1.

当您有多个 figure window 并希望将 axes 添加到不是最顶层的特定 figure 时,这是必需的。例如:

figure1 = figure;
figure2 = figure;

% Now you have two figure windows and you only want to add an axes to figure1.
% Note that figure2 is the topmost figure since it is created at a later time.
axes1 = axes('Parent', figure1);

如果你只有一个图形window或者图形window需要一个新的坐标轴是最上面的,你可以简单输入axes。例如:

figure1 = figure;
figure2 = figure;

% add a new axes to figure2.
axes1 = axes;

关于语法的更多信息

这样的语法在 MATLAB 中并不少见。例如:

figure('Color',[0 0 0], ....
              'Position', [0 0 100 100]);

scatter(x,y,'MarkerEdgeColor',[0 .5 .5],...
              'MarkerFaceColor',[0 .7 .7],...
              'LineWidth',1.5);

没错,你的理解是正确的。该行:

axes1 = axes(‘Parent’, figure1)

在变量axes1下创建一组坐标轴,并指定这组坐标轴的父对象是变量figure1下的图形对象,这可能是你正确的图形猜对了

当您同时创建和处理多个 figures/axes 时,明确指定哪个图形是哪个轴对象的父对象会很有用,这样您就可以知道哪个是哪个。