Calculate the curl of a vector field in Python and plot it with Matplotlib

To calculate the curl of a vector field in Python and plot it with Matplotlib, we can use the quiver() method to visualize the vector field and its curl components in 3D space.

What is Curl?

The curl of a vector field F = (u, v, w) measures the rotation or circulation of the field at each point. For a 3D vector field, the curl is calculated as:

curl F = (?w/?y - ?v/?z, ?u/?z - ?w/?x, ?v/?x - ?u/?y)

Example: Calculating and Plotting Curl

Let's create a vector field and visualize its curl using Matplotlib's 3D plotting capabilities ?

import matplotlib.pyplot as plt
import numpy as np

# Set up the figure
plt.rcParams["figure.figsize"] = [10, 8]
plt.rcParams["figure.autolayout"] = True

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

# Create coordinate meshgrid
x, y, z = np.meshgrid(np.arange(-0.8, 1, 0.2),
                      np.arange(-0.8, 1, 0.2),
                      np.arange(-0.8, 1, 0.4))

# Define vector field components
u = 0           # x-component
v = y**2        # y-component  
w = -2*y*z - y  # z-component

# Plot the vector field
ax.quiver(x, y, z, u, v, w, length=0.1, color='blue', alpha=0.7)

# Set labels and title
ax.set_xlabel('X')
ax.set_ylabel('Y') 
ax.set_zlabel('Z')
ax.set_title('Vector Field Visualization')

plt.show()

Calculating Curl Components

For the vector field F = (0, y², -2yz - y), we can calculate the curl components analytically ?

import matplotlib.pyplot as plt
import numpy as np

# Create meshgrid
x, y, z = np.meshgrid(np.arange(-1, 1.2, 0.4),
                      np.arange(-1, 1.2, 0.4), 
                      np.arange(-1, 1.2, 0.4))

# Original vector field
u = np.zeros_like(x)  # u = 0
v = y**2              # v = y²
w = -2*y*z - y        # w = -2yz - y

# Calculate curl components
# curl_x = ?w/?y - ?v/?z = (-2z - 1) - 0 = -2z - 1
# curl_y = ?u/?z - ?w/?x = 0 - 0 = 0  
# curl_z = ?v/?x - ?u/?y = 0 - 0 = 0

curl_x = -2*z - 1
curl_y = np.zeros_like(y)
curl_z = np.zeros_like(z)

# Create subplots
fig = plt.figure(figsize=(15, 5))

# Plot original vector field
ax1 = fig.add_subplot(131, projection='3d')
ax1.quiver(x, y, z, u, v, w, length=0.2, color='blue')
ax1.set_title('Original Vector Field')
ax1.set_xlabel('X')
ax1.set_ylabel('Y')
ax1.set_zlabel('Z')

# Plot curl field
ax2 = fig.add_subplot(132, projection='3d')
ax2.quiver(x, y, z, curl_x, curl_y, curl_z, length=0.2, color='red')
ax2.set_title('Curl of Vector Field')
ax2.set_xlabel('X')
ax2.set_ylabel('Y')
ax2.set_zlabel('Z')

# Plot both together
ax3 = fig.add_subplot(133, projection='3d')
ax3.quiver(x, y, z, u, v, w, length=0.2, color='blue', alpha=0.6, label='Original')
ax3.quiver(x, y, z, curl_x, curl_y, curl_z, length=0.2, color='red', alpha=0.8, label='Curl')
ax3.set_title('Combined View')
ax3.set_xlabel('X')
ax3.set_ylabel('Y')
ax3.set_zlabel('Z')

plt.tight_layout()
plt.show()

Key Parameters

When using quiver() for vector field visualization:

  • length: Controls arrow length scaling
  • color: Sets arrow colors
  • alpha: Controls transparency
  • projection='3d': Enables 3D plotting

Conclusion

Using Matplotlib's quiver() method, we can effectively visualize vector fields and their curl components in 3D space. The curl reveals the rotational characteristics of the field, helping understand fluid flow patterns and electromagnetic field behavior.

Updated on: 2026-03-25T22:55:19+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements