Animations

Animating Time-Varying Phenomena

Gradient descent converging to a minimum, a constellation rotating due to frequency offset, BER improving as you add more training symbols β€” these are all time-varying processes best understood through animation. Matplotlib's FuncAnimation creates MP4 and GIF files; Plotly's animation_frame does the same in HTML.

Definition:

FuncAnimation β€” Frame-by-Frame Animation

matplotlib.animation.FuncAnimation calls an update function for each frame:

from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
line, = ax.plot([], [])

def init():
    ax.set_xlim(0, 2*np.pi)
    ax.set_ylim(-1.5, 1.5)
    return line,

def update(frame):
    x = np.linspace(0, 2*np.pi, 200)
    line.set_data(x, np.sin(x + frame * 0.1))
    return line,

anim = FuncAnimation(fig, update, frames=100,
                     init_func=init, blit=True, interval=50)
anim.save('wave.gif', writer='pillow', fps=20)

Definition:

ArtistAnimation β€” Pre-Rendered Frames

ArtistAnimation takes a list of pre-rendered artist lists:

from matplotlib.animation import ArtistAnimation

fig, ax = plt.subplots()
frames = []
for i in range(50):
    line, = ax.plot(x, np.sin(x + i*0.1), color='blue')
    frames.append([line])

anim = ArtistAnimation(fig, frames, interval=50, blit=True)

Use FuncAnimation for memory-efficient animations (one frame at a time); use ArtistAnimation when all frames are already computed.

Theorem: Blitting for Smooth Animations

With blit=True, Matplotlib only redraws the changed artists each frame, not the entire figure. For NstaticN_{\text{static}} static elements and NdynamicN_{\text{dynamic}} animated elements:

Tframe∝{Nstatic+Ndynamicblit=FalseNdynamicblit=TrueT_{\text{frame}} \propto \begin{cases} N_{\text{static}} + N_{\text{dynamic}} & \text{blit=False} \\ N_{\text{dynamic}} & \text{blit=True} \end{cases}

Blitting gives 5-20x speedup when backgrounds are complex (grids, legends, annotations).

Blitting saves a snapshot of the static background and only re-renders the moving elements on top.

Example: Animating Gradient Descent Convergence

Animate gradient descent on the Rosenbrock function, showing the path converging to the minimum.

Example: Animating BER Estimate Convergence

Animate how the BER estimate stabilizes as more bits are simulated.

Wave Propagation Animation

Watch a sinusoidal wave propagate through space, reflecting the physical wave phenomena in wireless channels.

Parameters

Convergence Rate Visualizer

Compare how fast different optimization algorithms converge on a 2D cost function.

Parameters

Common Mistake: Animations Not Displaying in Jupyter

Mistake:

Creating a FuncAnimation in Jupyter without setting the HTML representation, resulting in just a static first frame.

Correction:

Use %matplotlib widget or convert to HTML:

from IPython.display import HTML
HTML(anim.to_jshtml())

Key Takeaway

Use animations sparingly but effectively. They are best for showing convergence processes, time-varying signals, and parameter sweeps. Always provide a static "final frame" alternative for papers. Save as GIF for presentations, MP4 for quality.

Quick Check

What does blit=True do in FuncAnimation?

Adds blur to smooth the animation

Only redraws changed artists each frame, skipping static elements

Doubles the frame rate

Compresses the output file

FuncAnimation

A Matplotlib class that creates animations by calling an update function for each frame, supporting blitting for performance.

Blitting

An animation optimization technique that caches the static background and only redraws dynamic elements each frame.