Different behaviour of hold on depending on order

1 回表示 (過去 30 日間)
Sergei Shestakov
Sergei Shestakov 2020 年 7 月 23 日
編集済み: Sergei Shestakov 2020 年 7 月 23 日
I'm not sure if it is a bug or feature per se, but it was certainly an unexpected behaviour.
Two following figures produce different output
x = 0:0.1:2*pi;
y = sin(x);
z = cos(x);
figure
plot(x, y)
hold on
plot(x, z)
figure
hold on
plot(x, y)
plot(x, z)
The difference is in presence of "border" on right and upper sides of picture.
Could someone explain to me the reason for this differences?
As far as I understand
figure
plot(x, y)
produces a picture with border, and hold on after that doesn't change the fact.
On the other hand
figure
does not create axes, after that
hold on
creates axes for some reason without the border, after that all plots do not change it for some reason.
Is there an actual way to hardcode the border (or the absence of it) in commands? Why does it work the way it works?

採用された回答

Arthur Roué
Arthur Roué 2020 年 7 月 23 日
編集済み: Arthur Roué 2020 年 7 月 23 日
Didn't know you can create axes with hold on command.
This kind of command, such as axis equal, change properties of your graphic object (see documenation of the command to know which one). You can access thoses properties directly with the axe handle.
x = 0:0.1:2*pi;
y = sin(x);
z = cos(x);
% Create figure
hFig = figure();
% Create an axe. The property 'NextPlot' is the one changing from 'replace'
% to 'add' when calling 'hold on' command
hAxe = axes(hFig, ...
'NextPlot', 'add', ...
'Box', 'off'); % 'off' without border (your 2nd case) and 'on' with border (your 1st case)
% Plot in the axe
plot(x, y)
plot(x, z)

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2020 年 7 月 23 日
Good catch! From the document of hold
"If axes do not exist, then the hold command creates them."
Without an existing axes, "hold on" creates an axes the same way as you run "axes".
The answer to this particular instance is that plot() creates an axes with different options than the default axes() command.
  1 件のコメント
Sergei Shestakov
Sergei Shestakov 2020 年 7 月 23 日
編集済み: Sergei Shestakov 2020 年 7 月 23 日
I couldn't believe for a minute that "hold on" and "plot" may possibly have different default options, but it seems that this is the case.
Arthur's answer provides a better alternatve to "hold on" I think.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by