Search code examples
octaveaxes

Octave axes limits change back to auto after plotting


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?


Solution

  • 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.