Plotting animated quivers in Python using Matplotlib

To animate quivers in Python, we can create dynamic vector field visualizations using Matplotlib's FuncAnimation. This technique is useful for showing changing vector fields over time, such as fluid flow or electromagnetic fields.

Steps to Create Animated Quivers

  • Set the figure size and adjust the padding between and around the subplots
  • Create x and y data points using numpy
  • Create u and v data points using numpy for vector components
  • Create a figure and a set of subplots
  • Plot a 2D field of arrows using quiver() method
  • To animate the quiver, change the u and v values in animate() method
  • Use FuncAnimation() to create the animation loop
  • Display the figure using show() method

Example

Here's a complete example that creates an animated quiver plot with changing vector directions and colors ?

import numpy as np
import random as rd
from matplotlib import pyplot as plt, animation

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create coordinate grids
x, y = np.mgrid[:2 * np.pi:10j, :2 * np.pi:5j]
u = np.cos(x)
v = np.sin(y)

# Create figure and subplot
fig, ax = plt.subplots(1, 1)
qr = ax.quiver(x, y, u, v, color='red')

def animate(num, qr, x, y):
    # Update vector components with time-dependent values
    u = np.cos(x + num * 0.1)
    v = np.sin(y + num * 0.1)
    qr.set_UVC(u, v)
    # Change color randomly for visual effect
    qr.set_color((rd.random(), rd.random(), rd.random(), rd.random()))
    return qr,

# Create animation
anim = animation.FuncAnimation(fig, animate, fargs=(qr, x, y),
                               interval=50, blit=False)

plt.show()

The output shows an animated vector field where arrows continuously change direction and color ?

[Animated quiver plot with rotating vectors and changing colors]

Key Components

Vector Field Setup

The np.mgrid creates coordinate grids, while u and v define the vector components at each point.

Animation Function

The animate() function updates vector components using set_UVC() and changes colors with set_color(). The num parameter represents the frame number.

FuncAnimation Parameters

  • fig − The figure to animate
  • animate − The function called for each frame
  • fargs − Additional arguments passed to animate function
  • interval − Delay between frames in milliseconds
  • blit − Whether to use blitting for optimization

Conclusion

Animated quiver plots provide an effective way to visualize time-varying vector fields. Use set_UVC() to update vector components and FuncAnimation to create smooth animations with customizable frame rates.

Updated on: 2026-03-25T22:32:52+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements