Contour Plot

Magnetic Field Contours

Contour visualization of magnetic field strength around a dipole.

Output
Magnetic Field Contours
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

x = np.linspace(-3, 3, 200)
y = np.linspace(-3, 3, 200)
X, Y = np.meshgrid(x, y)

# Magnetic dipole field
r = np.sqrt(X**2 + Y**2) + 0.1
theta = np.arctan2(Y, X)
B = (1/r**3) * np.sqrt(1 + 3*np.cos(theta)**2)
B = np.clip(B, 0, 10)

fig, ax = plt.subplots(figsize=(10, 8), facecolor='#ffffff')
ax.set_facecolor('#ffffff')

# Custom colormap: blue gradient
colors = ['#eff6ff', '#93c5fd', '#276CF5', '#1e3a8a']
cmap = LinearSegmentedColormap.from_list('blue', colors, N=256)

cs = ax.contourf(X, Y, B, levels=25, cmap=cmap)
ax.contour(X, Y, B, levels=12, colors='#1e3a8a', linewidths=0.5, alpha=0.5)
cbar = plt.colorbar(cs, ax=ax, pad=0.02)
cbar.set_label('Field Strength (T)', color='#374151', fontsize=11)
cbar.ax.yaxis.set_tick_params(color='#374151')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#374151')

ax.set_xlabel('X (m)', fontsize=11, color='#374151', fontweight='500')
ax.set_ylabel('Y (m)', fontsize=11, color='#374151', fontweight='500')
ax.set_title('Magnetic Field Contours', fontsize=14, color='#1f2937', fontweight='bold', pad=15)

ax.tick_params(colors='#6b7280', labelsize=9)
for spine in ax.spines.values():
    spine.set_color('#d1d5db')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Pairwise Data

Did this help you?

Support PyLucid to keep it free & growing

Support