# How do I show a line being created (like an animation) between two points inside a plot?

40 views (last 30 days)
RevengeOfTee on 28 Jan 2020
Edited: Adam Danz on 24 Jan 2021
Say I want to create a line between two points and want to show the line taking every step from the first point to the second point inside my plot. How would I go about creating this? For context, I am working on a robotics assignment that requires me to do this with a bug algorithm. I don't want a solution for the algorithm as it would take the fun out of it. Rather, I am just looking for a simple way of showing every step the line takes to join the two points. I currently have no idea what kind of code that would involve but I heard through the grapevine to use a stepsize.
Thank you in advance for any help!

Adam Danz on 28 Jan 2020
Edited: Adam Danz on 24 Jan 2021
If needed, interpolate your data to get more or less data points. Here's an example
xvalues = 1:16;
yvalues = (1:16).^2;
newX = min(xvalues) : 0.01 : max(xvalues);
newY = interp1(xvalues, yvalues, newX);
There are several methods to animate a plot. They all involve updating the plot within a loop, using drawnow. Note that this command updates all existing graphics so if lots of other plots/GUIs/Apps are opened all of them will be updated each time drawnow is called which will add a lot of processing time. To reduce this, remove unneeded graphics prior to animation.
Use drawnow limitrate to speed up the animation by skipping some of the graphics updates.
Use pause(n) to slow down the animation by inserting short pauses at the end of each iteration: pause(0.1).
Method 1: use an animation technique
Use one of the animation methods such as animatedline.
% Create the data
x = linspace(0,10);
y = sin(3*x);
% Set up the axis
clf()
axh = axes();
h = animatedline(axh, 'color','b');
ylim(axh, [-1 1]) % set limits before animation
xlim(axh, [1 10]) % set limits before animation
% Animate
for i = 1:numel(x)
drawnow
end
See AnimatedLine Properties for additional properties of the animated line.
See this answer to implement this demo on a yyaxis.
Method 2: update line coordinates iteratively
The most efficient approach for this method is to produce the line object with all NaN values and then to update the XData, YData, (and ZData, if needed) within the loop.
% Create the data
x = linspace(0,10);
y = sin(3*x);
% Set up the axis
clf()
axh = axes();
h = plot(axh, nan(size(x)),nan(size(y)),'b-'); % nothing will appear in the plot
ylim(axh, [-1 1]) % set limits before animation
xlim(axh, [1 10]) % set limits before animation
% Animate
for i = 1:numel(x)
h.XData(i) = x(i);
h.YData(i) = y(i);
drawnow
% pause(0.1) to slow it down
end
Another method is to plot independent line segments whose properties can be manipulated independently.
% Create the data
x = linspace(-2*pi,2*pi,300);
y = sin(x);
% Break line into segments
x = x(:); % Force col vectors
y = y(:); % Force col vectors
xseg = [x(1:end-1),x(2:end)];
yseg = [y(1:end-1),y(2:end)];
% Set up animation
clf()
hold on % important
xlim([-8 8])
ylim([-1 1])
segColors = jet(size(xseg,1)); % Choose a colormap
% Animate line
for i = 1:size(xseg,1)
h = plot(xseg(i,:),yseg(i,:),'-','LineWidth',3, 'color',segColors(i,:));
drawnow
end
Method 3: Create a movie
This uses getframe to store an image of each frame within a loop and movie to play it back. Otherwise, it's the same as method 2 above.
% Create the data
x = linspace(0,10);
y = sin(3*x);
% Set up the axis
clf()
axh = axes();
h = plot(axh, nan(size(x)),nan(size(y)),'b-'); % nothing will appear in the plot
ylim(axh, [-1 1]) % set limits before animation
xlim(axh, [1 10]) % set limits before animation
% Animate
n = numel(x);
F(n) = struct('cdata',[],'colormap',[]);
for i = 1:n
h.XData(i) = x(i);
h.YData(i) = y(i);
drawnow
F(i) = getframe;
end
Now play it back twice. Save F to play the movie again without recreating the data and figure.
movie(F,2)
RevengeOfTee on 28 Jan 2020
Thanks again!

R2019a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!