3D Stem

Fourier Series Coefficients

Complex Fourier coefficients of a square wave showing harmonic magnitudes.

Output
Fourier Series Coefficients
Python
import matplotlib.pyplot as plt
import numpy as np

# Fourier series coefficients
n = 20
k = np.arange(1, n + 1)

# Square wave Fourier coefficients
a_k = np.where(k % 2 == 1, 4 / (np.pi * k), 0)
phase = np.zeros(n)
phase[::2] = np.pi / 2

x = k
y = a_k * np.cos(phase)
z = a_k * np.sin(phase)

fig = plt.figure(figsize=(10, 8), facecolor='#0a0a0f')
ax = fig.add_subplot(111, projection='3d', facecolor='#0a0a0f')

markerline, stemlines, baseline = ax.stem(x, y, z, linefmt='-', markerfmt='o', basefmt=' ')
plt.setp(stemlines, color='#f59e0b', linewidth=2, alpha=0.8)
plt.setp(markerline, color='#fbbf24', markersize=8)

ax.set_xlabel('Harmonic (k)', color='white', fontsize=10)
ax.set_ylabel('Real Part', color='white', fontsize=10)
ax.set_zlabel('Imaginary Part', color='white', fontsize=10)
ax.set_title('Fourier Series Coefficients', color='white', fontsize=14, fontweight='bold', pad=20)

ax.tick_params(colors='#64748b', labelsize=8)
ax.xaxis.pane.fill = False
ax.yaxis.pane.fill = False
ax.zaxis.pane.fill = False
ax.xaxis.pane.set_edgecolor('#1e293b')
ax.yaxis.pane.set_edgecolor('#1e293b')
ax.zaxis.pane.set_edgecolor('#1e293b')

ax.view_init(elev=20, azim=45)
plt.tight_layout()
plt.show()
Library

Matplotlib

Category

3D Charts

Did this help you?

Support PyLucid to keep it free & growing

Support