Why does creating a new plot reset my axes properties?

12 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2019 年 3 月 21 日
回答済み: MathWorks Support Team 2019 年 3 月 25 日
I am running into an issue where I set some axes properties in my figure window, but then when I go to plot the axes is reset to it's default properties. Why is this happening?
How can I set the axes properties before plotting and have them stay in effect?
Please see the following simplified reproduction code:
f = figure();
ax = axes(f);
ax.Tag = 'MyAxesTag';
[X,Y,Z] = sphere(16);
x = [0.5*X(:); 0.75*X(:); X(:)];
y = [0.5*Y(:); 0.75*Y(:); Y(:)];
z = [0.5*Z(:); 0.75*Z(:); Z(:)];
disp(['The Axes Tag before scatter3 is: ', ax.Tag])
scatter3(ax,x,y,z)
disp(['The Axes Tag after scatter3 is: ', ax.Tag])

採用された回答

MathWorks Support Team
MathWorks Support Team 2019 年 3 月 21 日
This behavior is expected, the 'plot' command resets the axes properties before plotting.
There are two ways of resolving this:
  1. You can change this behavior using the 'hold' command,
  2. You can manually toggle the NextPlot property to 'replacechildren' or 'add' (this is how 'hold on' is implemented).
% Example 1 - Using the hold command
f = figure();
ax = axes(f);
ax.Tag = 'MyAxesTag';
[X,Y,Z] = sphere(16);
x = [0.5*X(:); 0.75*X(:); X(:)];
y = [0.5*Y(:); 0.75*Y(:); Y(:)];
z = [0.5*Z(:); 0.75*Z(:); Z(:)];
disp(['The Axes Tag before scatter3 is: ', ax.Tag])
hold(ax,'on')
scatter3(ax,x,y,z)
disp(['The Axes Tag after scatter3 is: ', ax.Tag])
The hold command allows for the retention of the current plots when creating new plots. For more information, please see:
% Example 2 - using the NextPlot property
f = figure();
ax = axes(f);
ax.Tag = 'MyAxesTag';
[X,Y,Z] = sphere(16);
x = [0.5*X(:); 0.75*X(:); X(:)];
y = [0.5*Y(:); 0.75*Y(:); Y(:)];
z = [0.5*Z(:); 0.75*Z(:); Z(:)];
disp(['The Axes Tag before scatter3 is: ', ax.Tag])
ax.NextPlot = 'add'
scatter3(ax,x,y,z)
disp(['The Axes Tag after scatter3 is: ', ax.Tag])

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by