HOWTO · MATLAB
How to Create Animated Plot in MATLAB
Learn how to create animated plots in MATLAB using the drawnow command and pause function. This guide provides step-by-step methods, practical examples, and tips for visualizing data dynamically. Enhance your data presentation and engage your audience with animated plots in MATLAB.
On this page
Use drawnow inside a loop to refresh a MATLAB figure as data changes. Add pause() when you want a visible delay; it accepts seconds and does not replace the graphics update. The examples use Octave-compatible syntax shared with MATLAB and are provided for adaptation; this packet contains no execution results for them.
Animate a MATLAB Plot with drawnow
This example reveals a sine curve from left to right. The loop extends the plotted data on each iteration, axis keeps the view fixed, and drawnow asks MATLAB to render the update. The blue line and labels make the changing data easy to follow.
Run this example:
x = 0:0.1:10;
y = sin(x);
warning('off', 'Octave:gnuplot-graphics');
figure;
plot(x(1), y(1), 'b-');
axis([0 10 -1 1]);
title('Animating a Sine Wave');
xlabel('X');
ylabel('sin(X)');
for k = 2:length(x)
plot(x(1:k), y(1:k), 'b-');
axis([0 10 -1 1]);
title('Animating a Sine Wave');
xlabel('X');
ylabel('sin(X)');
drawnow;
pause(0.02);
end
fprintf(1, 'Animation complete.\n');
The first point creates the figure before the loop. Each later pass draws the curve through the current point. Change the range in x and the matching horizontal limit in axis together; change the vertical limits when your data falls outside the current range. MATLAB’s plot options let you adjust line color, marker, and width.
Control Animation Timing with pause
drawnow updates the displayed graphics; pause(seconds) delays the loop. For example, pause(0.05) requests a 0.05-second wait at each iteration. A shorter delay can make the animation appear faster and a longer delay slower, but the actual pace also depends on computation and rendering, so this value does not guarantee a frame rate. To display each update without a deliberate delay, omit pause() and keep drawnow.
Animate Multiple Data Series in One Figure
Create each line once, then update its data in the loop. This keeps both series in one axes and avoids recreating the legend on every pass. The legend labels distinguish the sine and cosine curves.
x = 0:0.1:10;
warning('off', 'Octave:gnuplot-graphics');
figure;
hold on;
sine_line = plot(x(1), sin(x(1)), 'b-', 'LineWidth', 2);
cosine_line = plot(x(1), cos(x(1)), 'r-', 'LineWidth', 2);
axis([0 10 -1 1]);
title('Sine and Cosine');
xlabel('X');
ylabel('Value');
legend('Sine', 'Cosine');
for k = 2:length(x)
set(sine_line, 'XData', x(1:k), 'YData', sin(x(1:k)));
set(cosine_line, 'XData', x(1:k), 'YData', cos(x(1:k)));
drawnow;
pause(0.02);
end
hold off;
fprintf(1, 'Animation complete with two series.\n');
For your own data, replace the two expressions with arrays of matching values and update both lines to the same current index. Keep distinct colors or line styles and retain a legend so the series remain identifiable.
Save or Extend the Animation
In MATLAB, VideoWriter can save frames from an animation as a video. Open a writer for the output file, capture each rendered frame with getframe, pass it to writeVideo, and close the writer when the loop finishes. Adapt the captured figure and output settings to your MATLAB installation; video writing depends on MATLAB and the selected video profile, and this workflow is not demonstrated by the examples above.
To animate another 2D plot, update its data inside the same loop and call drawnow. Keep fixed axis limits when you want a stable view, or recalculate them when the changing data should determine the view. For a 3D line, use plot3 and update the x, y, and z data together.
FAQ
How can I control the animation speed?
Adjust or remove the delay passed to pause(). It is measured in seconds, and the perceived pace also depends on calculation and rendering time.
Can I save an animated plot as a video?
Yes. MATLAB’s VideoWriter workflow captures frames and writes them to a video file; see Save or Extend the Animation for the steps and adaptation notes.
Can I make a 3D animated plot?
Yes. Use plot3 and update all three coordinate arrays in the loop, then call drawnow to refresh the figure.
Can I animate other plot types?
Yes. Update the data or graphics object for a scatter plot, bar chart, or another supported plot, and call drawnow after the update. The object properties you change depend on the plot type.
What is the difference between drawnow and pause()?
drawnow refreshes graphics so changes appear in the figure. pause() waits for a duration in seconds. Use both when you need visible updates with an intentional delay.