In [55]:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Setting up the axes
x, y, z = np.meshgrid(np.arange(-3, 3, 0.1), np.arange(-3, 3, 0.1), np.arange(-3, 3, 0.1))
#Defining the EM wave and its physical parameters
k = 2
w = 1
E_x = 0
E_y = np.sin(np.pi*z)*np.cos(k*x)
E_z = 0
#Plotting the EM wave in space
c = np.abs(E_y)
# Flatten and normalize
c = (c.ravel() - c.min()) / c.ptp()
# Repeat for each body line and two head lines
c = np.concatenate((c, np.repeat(c, 2)))
# Colormap
c = plt.cm.inferno(c)
ax = plt.figure().add_subplot(projection = '3d')
EM_wave = plt.quiver(x, y, z, E_x, E_y, E_z, color = c, length = 0.1, normalize = True, cmap = 'inferno')
plt.colorbar()
plt.show()
#was thinking of animating it too but considering how long it took just to render one snapshot of the EM wave, I think
#Python is too high-level for that, for speed you would definitely want to use a compiled programming language instead.
Essentially, think of "cylinders" of light that, if animated with time, would be seen propagating in the $x$ direction.