10.12: Animated Plots

Fundamentals of MATLAB Programming · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    Read aloud

    How

    Speed: 1×
    Light up
    Highlight

    Symbols: Mulberry Symbols, © 2018–2026 Steve Lee, CC BY-SA 4.0 (licence, the set), the site's own copies.

    From LibreTexts, under CC BY-NC-SA, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    10.12: Animated Plots

    Sometimes we want to show how a graph is drawn over time. MATLAB provides several ways to create animations. Two beginner-friendly options are comet and movie.

    Using comet

    The comet function animates a curve as if a point is moving along the path.

    Example \(\PageIndex{1}\)

    Using comet to create an animated plot.

    clear; clc; clf;
    x = -2*pi:1/5:2*pi;
    y = sin(x);
    
    comet(x, y)
    xlabel('x')
    ylabel('sin(x)')
    title('Animated sin(x)')
    Solution

    Run the code in MATLAB to see how the plot is animated.

    clipboard_e773c4796398deccc2d8238d11f3ff3eb.png


    Using movie

    MATLAB plots one point at a time, captures each frame using getframe, and then plays the frames as a movie.

    Example \(\PageIndex{2}\)

    Using comet to create an animated plot.

    clear; clc; clf;
    
    x = -2*pi:1/5:2*pi;
    y = sin(x);
    n = length(x);
    
    for i = 1:n
        plot(x(i), y(i), 'r*')
        axis([min(x)-1 max(x)+1 min(y)-1 max(y)+1])
        M(i) = getframe;
    end
    
    movie(M)
    Solution

    Run the code in MATLAB to see how the plot is animated by moving the a red star.


    This page titled 10.12: Animated Plots was last modified on Tue, 30 Jun 2026 02:11:46 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.