I am experimenting with octave animations and I have an issue with the following code:

clear
x = 0:pi/1000:2*pi;
y = sin(x);
y2 = sin(2*x);
y3 = sin(3*x);
figure
xlim("manual");
ylim("manual");
xlim([0 2*pi]);
ylim([-1 1]);
tic
for i = 1:2000
  xlim ("mode")
  plot(x(i),y(i),'b',x(i),y2(i),'r',x(i),y3(i),'g')
  pause(1)
end
toc

At the output I get:

ans = manual
ans = auto
ans = auto
ans = auto
ans = auto

Why is the axis mode reverting to auto after plotting new data?

  • Indeed, anyway round it short of resetting the axis limits inside the loop? – Tim Mottram 7 hours ago
  • @CrisLuengo really? must be new, this has been default behaviour for a while as far as I know ... – Tasos Papastylianou 4 hours ago
  • 1
    @Tasos: sorry, I mis-read the code, I assumed there was a hold on there. You are right, plot here is supposed to reset the axes. – Cris Luengo 2 hours ago
up vote 2 down vote accepted

This is indeed intended behaviour. A good rationale for it being that there is no reason to assume that subsequent independent calls to the plot function should somehow be related, therefore octave chooses the best possible representation that fits the data. Therefore the fact that the calls to 'plot' in your case in your plotting strategy happen to be 'related' is inconsequential.

If you want to keep the previous axis settings etc within your loop, there are several options.

  • You may simply keep setting the limits at the end of each iteration, as you suggest
  • Rather than create a new axes object each time you call plot, you can hold on and plot things on the same axes; if you keep a record of the handle for each plot, you can delete the previous one as necessary leaving only the last one showing.
  • Plot only once, and within your loop simply replace the plot object's xdata and ydata fields, to update your plot.

Obviously the most straightforward thing to do is the first option; the last one might be something to consider if, e.g., computational efficiency is an issue.

  • Are you sure thats the case if I have set the axes to manual? As per this answer on mathworks: mathworks.com/matlabcentral/answers/… The suggestion was to use xlim and ylim commands to fix the axes between plots, OP then found that this still didn't work and in related questions the advice was to set the axes to manual. I set the axes to manual, I don't think it should just change back because I plotted more data on the same axes. – Tim Mottram 45 mins ago
  • @Tim: You didn't plot more data on the same axes. Add a hold on to add data to the same axes. Also, for animations you are better off changing the YData property of the lines drawn, instead of plotting the updated values over the old ones. – Cris Luengo 41 mins ago
  • Yeah I can't just use hold on because I don't want to see every plot - OK, I'll update the data. Out of interest then, whats the use of manual and auto? If any new plot resets the mode then why be able to change the mode (I don't need to change to manual to set a limit) – Tim Mottram 34 mins ago
  • Ok, I understand, I didn't realise that a new axes was being created every plot. – Tim Mottram 32 mins ago
  • @Tim: When you add a line using hold on; plot(...), the limits are adjusted to include all the data plotted unless the mode is set to manual. If hold off (the default), then plot clears the axes and resets all its properties (almost the same as deleting the axes and making new ones, except the handle is maintained). – Cris Luengo 14 mins ago

The right way to make an animation is to update the 'XData' and 'YData' properties of the line created by plot. Something like this:

x = 0:pi/1000:2*pi;
y = sin(x);
y2 = sin(2*x);
y3 = sin(3*x);
cla
h = plot(x(1),y(1),'b',x(1),y2(1),'r',x(1),y3(1),'g');
xlim([0 2*pi]);
ylim([-1 1]);
for i = 2:2000
  set(h(1),'XData',x(i),'YData',y(i))
  set(h(2),'XData',x(i),'YData',y2(i))
  set(h(3),'XData',x(i),'YData',y3(i))
  pause(0.01)
end

If you want to preserve the previous dots, you can modify the set commands as follows:

set(h(1),'XData',x(1:i),'YData',y(1:i))

This way, a new point is added, rather than moving the existing point.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.