axis changes even after "axis equal"

21 次查看(过去 30 天)
I would like to plot a line on an axis without changing the XLim and YLim values. I obviously went through the "axis equal" command (or the ax.XLimMode='manual'; ax.YLimMode='manual';) but this doesn't seems to work.
Where am I doing wrong?
plot(1:10)
% XLim is not [1 10]
axis manual
plot(1:20)
% XLim should be again [1 10], instead it's [0 20] now
it works with
plot(1:10)
% XLim is not [1 10]
hold on
axis manual
plot(1:20)
% XLim should be again [1 10], instead it's [0 20] now
but I don't want to hold previous plots (it's an animation).
A workaround could be to use
ph=plot(1:10)
axis manual
hold on
delete(ph)
plot(1:20)
but I this seems over-complicated to do the simple thing I'm asking to...
I'm working with matlab R2015a

采纳的回答

Chad Greene
Chad Greene 2015-4-13
If you don't hold the plots, axis limits will be reset every time you call plot. For an animation, you can use hold and either delete plotted objects or change their data. That is,
hold on
h = plot(1:10);
<grab animation frame here>
delete(h)
h = plot(1:20);
and so on. Or you can change the data in h with
set(h,'xdata',1:20,'ydata',1:20)
  1 个评论
Luca Amerio
Luca Amerio 2015-4-14
Yes, I already though to both these solutions. Was hoping for a simpler one because I need to teach this to students that are not familiar with MATLAB.
Using delete, set of h.XData=[...] are adding a further complication to a simple problem.
To bad: I was hoping the solution to be cleaner.

请先登录,再进行评论。

更多回答(1 个)

pfb
pfb 2015-4-14
编辑:pfb 2015-4-14
If I understand this correctly, you want an animation with fixed xlim, but you do not want to set the xdata and ydata (I agree with Chad Greene that this is the way to go, but I understand your commment about your students).
I guess that you're plotting plots in a loop, to form the animation. So why not setting the desired xlim after each plot, instead of holding it and deleting it?
It is not optimal, but it should not make much difference if you're grabbing frames for a movie.
If you are just "showing" a movie, it won't look very good, though. Maybe you can pause the loop for a tiny while after each xlim. I think you need also drawnow.
figure(1)
clf
for n=1:10
plot(1:10,(1:10)*n);
axis([1 10 1 100]);
drawnow;
pause(.2); %change this according to your needs
end
  2 个评论
Luca Amerio
Luca Amerio 2015-4-14
Thank you very much. This could be a doable workaround. I will decide if either this or delete(h) cause less questions.
Thank you very much.
pfb
pfb 2015-4-14
You're welcome. But I think you accepted the other answer :)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by