Hold on - best practice question
6 ビュー (過去 30 日間)
古いコメントを表示
I wonder if there is any real difference between two following versions of 'hold on' application:
- Case 1 - 'hold on' is used directly after the 'figure' declaration
- Case 2 - 'hold on' is used after the first plot
% DATA
x = 1:10;
y1 = x.^2;
y2 = x.^3;
% PLOTS
% Case 1 - 'hold on' after 'figure' declaration
figure
hold on;
plot(x, y1);
plot(x, y2);
% Case 2 - 'hold on' after 1st plot
figure
plot(x, y1);
hold on;
plot(x, y2);
- Is there any performance gain/loss between these two cases?
- Or it is entirely down to the preference of the person writing the code?
I'm asking as I noticed that many people use 'Case 1' style, which I personally find more elegant. but on the other hand, all the examples featured in the Matlab documentation use the 'Case 2' style.
To add a bit more into the mix, I came across this thread, where Jan Simon abstains from using 'hold on' function altogether in favour of using handles:
So, what is the 'best practice' in this regard?
3 件のコメント
Adam
2017 年 12 月 20 日
編集済み: Adam
2017 年 12 月 20 日
Putting hold on before you have plotted anything has often led to unwanted results (though I can't remember the circumstances or whether it was just older versions of Matlab) when I have done it so I would never do that personally.
Also I always use the axes handle explicitly as with any plotting instruction - i.e.
hold( hAxes, 'on' )
where hAxes is your axes handle.
Stephen23
2017 年 12 月 21 日
編集済み: Stephen23
2017 年 12 月 21 日
@Pawel Jastrzebski: if you are really interested in "best practice", then the most significant change you could make to your code is to obtain and use all graphics handles explicitly. This means getting the figure, axes, line, patch, image, etc. handles and always supplying them (e.g. as parent property) when calling all graphics functions.
When you get to the point of asking about hold best-practice, then you are at a good point to move away from the unreliable practice of assuming that the current figure/axes/... are always the correct ones to be accessing.
採用された回答
Jan
2017 年 12 月 20 日
編集済み: Jan
2017 年 12 月 20 日
Especially inside loops I do not like to call hold('on') repeatedly. Therefore I avoid this command in general, but set the property of the axes manually:
AxesH = axes('NextPlot', 'add');
This is what happens inside hold also. I admit that this looks a little bit "low level" and hold('on') might look easier. But this has the addition advantage, that a plot command does not skip formerly defined axes properties like limits or tickmarks.
And as code: Compare:
for k = 1:10
plot(1:10, rand(1, 10));
hold('on');
end
with:
AxesH = axes('NextPlot', 'add');
for k = 1:10
plot(AxesH, 1:10, rand(1, 10));
end
2 件のコメント
Jan
2017 年 12 月 21 日
Do you want to modify the figure or the axes? For the later, axes('NextPlot' 'add') is relevant, not figure('NextPlot', 'add'). As far as I understand the posted documentation, the property of figure controls, if a new figure is created for a new axes object, or if the existing figure is re-used.
See the newplot function also.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Graphics Object Properties についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
