Hold on - best practice question
26 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Pawel Jastrzebski
el 20 de Dic. de 2017
Comentada: Jan
el 21 de Dic. de 2017
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 comentarios
Adam
el 20 de Dic. de 2017
Editada: Adam
el 20 de Dic. de 2017
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
el 21 de Dic. de 2017
Editada: Stephen23
el 21 de Dic. de 2017
@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.
Respuesta aceptada
Jan
el 20 de Dic. de 2017
Editada: Jan
el 20 de Dic. de 2017
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 comentarios
Jan
el 21 de Dic. de 2017
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Graphics Object Properties en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
