Sait
Sait

Reputation: 19805

Octave plot points as animation

I have the following octave script,

TOTAL_POINTS = 100;
figure(1)

for i=1:TOTAL_POINTS
   randX = rand(1); 
   randY = rand(1);

   scatter(randX, randY);
   hold on; 
endfor

When I run this by octave script.m, I get nothing. When I add the line pause to the end of this script, it works but I only see the plot after all the 100 points are plotted.

I want to see the plot point by point. Not after I plotted every single point.

PS: I have Ubuntu.

Upvotes: 4

Views: 2592

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112659

Try adding drawnow at the end of the iteration. At least that works in Matlab. It forces the interpreter to empty the graphic event queue.

If you include pause at the end of the iteration, drawnow may not be needed (pause already empties the event queue). But I usually add it just in case.

Other comments:

  • You don't need hold on at every iteration. It sufficies to do it once, at the beginning.
  • You may want to freeze the axis to prevent auto-scaling when a new point is added

With these modifications, your code would become:

TOTAL_POINTS = 100;
figure(1)
hold on; 
axis([0 1 0 1]) %// set axis
axis manual %// prevent axis from auto-scaling
for i=1:TOTAL_POINTS
   randX = rand(1); 
   randY = rand(1);
   scatter(randX, randY);
   pause(.1) %// pause 0.1 seconds to slow things down
   drawnow
endfor

Upvotes: 6

Related Questions