Reputation: 12134
So I have a plot of N points in the 2D plane (N can be very large). I am writing a script that is to show the workings of an algorithm. So I have for loops. At each step in the for loop I'd like to change the color of the current point (actually probably make a stem plot with just this point).
However, at the end of the step I'd like to remove the coloring of the current point so that I can color the next one. Currently I have to redraw the whole plot (incl. the 2D points). I'm not sure whether Matlab detects such things in the plotting commands but is there a way to do this without redrawing the whole plot?
For example:
plot(x,y, '*');
for j = 1:N-1
for i = j:N
hold on;
%Do stuff
plot(x,y, '*');
hold on;
stem(x(1), y(1), 'g*');
end
end
Upvotes: 2
Views: 4571
Reputation: 124563
A quick example:
%# plot some data
x = 1:100;
y = cumsum(rand(size(x))-0.5);
plot(x,y,'*-')
%# animate by going through each point
hold on
h = stem(x(1),y(1),'g');
hold off
for i=1:numel(x)
%# update the stem x/y data
set(h, 'XData',x(i), 'YData',y(i));
%# slow down a bit, drawnow was too fast!
pause(.1)
end
Upvotes: 7
Reputation: 12693
Take a look at the documentation of handle graphics objects.
I'd recommend plotting the whole set of points as one object. Then, for each iteration, plot the point of interest. Save a handle to it (as in, h = plot(...);
). When you're ready for the next iteration, delete
the object using that handle (delete(h)
), and create the next one in the same manner.
%# outside the for loop (do this once)
plot(x,y,'*');
for...
h = stem(x(i),y(i),'g*');
...
%# next iteration... i has been incremented
delete(h);
h = stem(x(i),y(i),'g*');
end
Upvotes: 2