16
votes

I was wondering if anyone knew how to do an animation plot of x = (dataset of 1000 points) y = (dataset of 1000 points) plot(x,y)

big problem is these are datasets that i am trying to plot , or x,y coordinates as opposed to a function which I would know how to plot via an animation.

I tried to do frames in a for loop but it gave me dots and didn't join them in a line graph so I couldn't really watch the path being traced out.

code I used was

for i = 1:length(DATASET1)
pause(0.1)
plot(DATASET1(i),DATASET2(i))
draw on
end
3
What is the code you tried that didn't work? - darthbith
That's not a plot command.... You should edit the post (with the link just below the tags) to include all of the code that you have so far. You can format the code by clicking the {} button, pressing CTRL-K or by indenting it by 4 spaces - darthbith
Thank you so so sorry for not doing that.. - branny12000
for some reason I cant put the code in that format on a comment. I can if I go to answer the question but I have to wait 8 hours before I can do that.. sorry I will try and include it in my question - branny12000
It is preferred that you edit it into the question rather than post it in a comment :-) Well done fixing up your question - darthbith

3 Answers

36
votes

If what you want is for the plot to "grow" point by point: the easiest way is to create an empty plot and then update its XData and YData properties at each iteration:

h = plot(NaN,NaN); %// initiallize plot. Get a handle to graphic object
axis([min(DATASET1) max(DATASET1) min(DATASET2) max(DATASET2)]); %// freeze axes
%// to their final size, to prevent Matlab from rescaling them dynamically 
for ii = 1:length(DATASET1)
    pause(0.01)
    set(h, 'XData', DATASET1(1:ii), 'YData', DATASET2(1:ii));
    drawnow %// you can probably remove this line, as pause already calls drawnow
end

Here's an example1 obtained with DATASET1 = 1:100; DATASET2 = sin((1:100)/6);

enter image description here


1 In case someone's interested, the figure is an animated gif which can be created by adding the following code (taken from here) within the loop, after the drawnow line:

  frame = getframe(1);
  im = frame2im(frame);
  [imind,cm] = rgb2ind(im,256);
  if ii == 1;
      imwrite(imind,cm,filename,'gif','Loopcount',inf);
  else
      imwrite(imind,cm,filename,'gif','WriteMode','append');
  end

9
votes

Looks like you were close. Not sure draw on is any command though.

See if the code here inspires you to solve your case -

%// Sample x and y values assumed for demo.
x = 1:1000;
y = x.^2;

%// Plot starts here
figure,hold on

%// Set x and y limits of the plot
xlim([min(x(:)) max(x(:))])
ylim([min(y(:)) max(y(:))])

%// Plot point by point
for k = 1:numel(x)
    plot(x(k),y(k),'-') %// Choose your own marker here

    %// MATLAB pauses for 0.001 sec before moving on to execue the next 
    %%// instruction and thus creating animation effect
    pause(0.001);     
end
5
votes

Since R2014b, you can work with annimatedline object (doc and how-to) that is meant to handle animated graphs pretty well. Basically, the annimatedline object has a addpoints function that adds new points to the line without having to redefine the existing points, along with a clearpoints function that clears lines for more complex animations.

Here is an example:

h = animatedline;
axis([0,4*pi,-1,1])

x = linspace(0,4*pi,1000);
y = sin(x);
for k = 1:length(x)  
    addpoints(h,x(k),y(k));
    drawnow
end