MATLAB Answers

Why does creating a new plot reset my axes properties?

15 views (last 30 days)
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])

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 21 Mar 2019
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 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2017a

Translated by