Python

 

 

 

 

Python -Plotting EM Wave Polarization

 

NOTE 1 : This example requires some additional package Panda, Numpy, matplotlib. Followings are the versions of Python and these packages that I used for this example.

 

 

Polarization.py =====================================================

 

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import mpl_toolkits.mplot3d.axes3d as axes3d

import matplotlib.gridspec as gridspec

 

def drawAxis(ax,max = 4*np.pi) :

    ax.plot([0,max],[0,0],[0,0],'k')

    ax.plot([0,0],[-2,2],[0,0],'g')

    ax.plot([0,0],[0,0],[-2,2],'b')

    return

 

def drawHwave(ax,max = 4*np.pi, offset = 0, Hshift = 0, Hamp = 1.0):

    t = np.linspace(0 + offset, max + offset, 100)

    x = t - offset

    y = Hamp * np.cos(t - Hshift)

    z = 0 * t

    ax.plot(x, y, z,'g')

    return

 

def drawVwave(ax,max = 4*np.pi, offset = 0, Vshift = 0, Vamp = 1.0):

    t = np.linspace(0 + offset, max + offset, 100)

    x = t - offset

    y = 0 * t

    z = Vamp * np.cos(t-Vshift)

    ax.plot(x, y, z,'b')

    return

 

def drawSumwave(ax,max = 4*np.pi, offset = 0, Hshift = 0, Vshift = 0, Hamp = 1.0, Vamp = 1.0):

    t = np.linspace(0 + offset, max + offset, 100)

    x = t - offset

    yH = 0 * t

    yV = Hamp * np.cos(t-Hshift)

    zH = 0 * t

    zV = Vamp * np.cos(t-Vshift)

    y = yH + yV

    z = zH + zV

    ax.plot(x, y, z,'r')

    return

 

def drawVectorSum(ax, offset = 0, Hshift = 0, Vshift = 0 , Hamp = 1.0, Vamp = 1.0):

    t = offset

    yH = 0 * t

    yV = Hamp * np.cos(t - Hshift)

    zH = 0 * t

    zV = Vamp * np.cos(t - Vshift)

    y = yH + yV

    z = zH + zV

    

    x1 = 0

    y1 = y

    z1 = 0

    x2 = 0

    y2 = y

    z2 = z

    ax.plot([x1,x2], [y1,y2], [z1,z2],'g--')

    

    x1 = 0

    y1 = 0

    z1 = z

    x2 = 0

    y2 = y

    z2 = z

    ax.plot([x1,x2], [y1,y2], [z1,z2],'b--')

    

    x1 = 0

    y1 = 0

    z1 = 0

    x2 = 0

    y2 = y

    z2 = z

    ax.plot([x1,x2], [y1,y2], [z1,z2],'r')

    ax.plot([x2],[y2],[z2],'ro')

        

    return

 

def drawPropagatingWave(ax,off = 0,Hoffset = 0, Voffset = 0, Ha = 1.0, Va = 1.0):

    drawAxis(ax, max = 4*np.pi)

    drawHwave(ax, max = 4*np.pi, offset = off,Hshift = Hoffset, Hamp = Ha)

    drawVwave(ax, max = 4*np.pi, offset = off,Vshift = Voffset, Vamp = Va)

    drawSumwave(ax, max = 4*np.pi, offset = off, Hshift  = Hoffset, Vshift = Voffset,

                Hamp = Ha, Vamp = Va)

    drawVectorSum(ax, offset = off,Hshift  = Hoffset, Vshift = Voffset, Hamp = Ha, Vamp = Va)

 

    ax.grid(False)

    ax.axis('off')

    ax.set_xticks([])

    ax.set_yticks([])

    ax.set_zticks([])

    return

 

def drawPhaseDiagram(ax,max = 2*np.pi, offset = 0, Hoffset = 0, Voffset = 0, Ha = 1.0, Va = 1.0):

    xmax = 1.2

    ymax = 1.2

    ax.plot([-xmax,xmax],[0,0],'g')

    ax.plot([0,0],[-ymax,ymax],'b')

    

    t = np.linspace(0 + offset, max + offset, 100)

    x = Ha * np.cos(t-Hoffset)

    y = Va * np.cos(t-Voffset)

    ax.plot(x,y,'k')

    

    t = offset

    x = Ha * np.cos(t-Hoffset)

    y = Va * np.cos(t-Voffset)

    ax.plot(x,y,'ro')

    ax.plot([x,x],[0,y],'g--')

    ax.plot([0,x],[y,y],'b--')

    ax.plot([0,x],[0,y],'r')

    

    ax.set_xlim(-xmax,xmax)

    ax.set_ylim(-ymax,ymax)

    ax.set_aspect('equal')

    ax.grid(False)

    ax.set_xticks([])

    ax.set_yticks([])

    return

 

fig = plt.figure(figsize=(12,5))

 

gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1])

 

ax1 = plt.subplot(gs[0],projection='3d')

ax2 = plt.subplot(gs[1])

 

 

startAngle = 0*np.pi/10

hOff = 1*np.pi/4.0

Hamp = 1.0

Vamp = 1.0

 

drawPropagatingWave(ax1,off = startAngle, Hoffset = hOff, Ha = Hamp, Va = Vamp)  

drawPhaseDiagram(ax2,max = 4*np.pi, offset = startAngle, Hoffset = hOff, Ha = Hamp, Va = Vamp)

 

plt.show()

 

 

 

Polarization_Animation.py =====================================================

 

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import mpl_toolkits.mplot3d.axes3d as axes3d

import matplotlib.gridspec as gridspec

import matplotlib.animation as animation

 

def drawAxis(ax,max = 4*np.pi) :

    ax.plot([0,max],[0,0],[0,0],'k')

    ax.plot([0,0],[-2,2],[0,0],'g')

    ax.plot([0,0],[0,0],[-2,2],'b')

    return

 

def drawHwave(ax,max = 4*np.pi, offset = 0, Hshift = 0, Hamp = 1.0):

    t = np.linspace(0 + offset, max + offset, 100)

    x = t - offset

    y = Hamp * np.cos(t - Hshift)

    z = 0 * t

    ax.plot(x, y, z,'g')

    return

 

def drawVwave(ax,max = 4*np.pi, offset = 0, Vshift = 0, Vamp = 1.0):

    t = np.linspace(0 + offset, max + offset, 100)

    x = t - offset

    y = 0 * t

    z = Vamp * np.cos(t-Vshift)

    ax.plot(x, y, z,'b')

    return

 

def drawSumwave(ax,max = 4*np.pi, offset = 0, Hshift = 0, Vshift = 0, Hamp = 1.0, Vamp = 1.0):

    t = np.linspace(0 + offset, max + offset, 100)

    x = t - offset

    yH = 0 * t

    yV = Hamp * np.cos(t-Hshift)

    zH = 0 * t

    zV = Vamp * np.cos(t-Vshift)

    y = yH + yV

    z = zH + zV

    ax.plot(x, y, z,'r')

    return

 

def drawVectorSum(ax, offset = 0, Hshift = 0, Vshift = 0 , Hamp = 1.0, Vamp = 1.0):

    t = offset

    yH = 0 * t

    yV = Hamp * np.cos(t - Hshift)

    zH = 0 * t

    zV = Vamp * np.cos(t - Vshift)

    y = yH + yV

    z = zH + zV

    

    x1 = 0

    y1 = y

    z1 = 0

    x2 = 0

    y2 = y

    z2 = z

    ax.plot([x1,x2], [y1,y2], [z1,z2],'g--')

    

    x1 = 0

    y1 = 0

    z1 = z

    x2 = 0

    y2 = y

    z2 = z

    ax.plot([x1,x2], [y1,y2], [z1,z2],'b--')

    

    x1 = 0

    y1 = 0

    z1 = 0

    x2 = 0

    y2 = y

    z2 = z

    ax.plot([x1,x2], [y1,y2], [z1,z2],'r')

    ax.plot([x2],[y2],[z2],'ro')

        

    return

 

def drawPropagatingWave(ax,off = 0,Hoffset = 0, Voffset = 0, Ha = 1.0, Va = 1.0):

    drawAxis(ax, max = 4*np.pi)

    drawHwave(ax, max = 4*np.pi, offset = off,Hshift = Hoffset, Hamp = Ha)

    drawVwave(ax, max = 4*np.pi, offset = off,Vshift = Voffset, Vamp = Va)

    drawSumwave(ax, max = 4*np.pi, offset = off, Hshift  = Hoffset, Vshift = Voffset, Hamp = Ha, Vamp = Va)

    drawVectorSum(ax, offset = off,Hshift  = Hoffset, Vshift = Voffset, Hamp = Ha, Vamp = Va)

 

    ax.grid(False)

    ax.axis('off')

    ax.set_xticks([])

    ax.set_yticks([])

    ax.set_zticks([])

    return

 

def drawPhaseDiagram(ax,max = 2*np.pi, offset = 0, Hoffset = 0, Voffset = 0, Ha = 1.0, Va = 1.0):

    xmax = 1.2

    ymax = 1.2

    ax.plot([-xmax,xmax],[0,0],'g')

    ax.plot([0,0],[-ymax,ymax],'b')

    

    t = np.linspace(0 + offset, max + offset, 100)

    x = Ha * np.cos(t-Hoffset)

    y = Va * np.cos(t-Voffset)

    ax.plot(x,y,'k')

    

    t = offset

    x = Ha * np.cos(t-Hoffset)

    y = Va * np.cos(t-Voffset)

    ax.plot(x,y,'ro')

    ax.plot([x,x],[0,y],'g--')

    ax.plot([0,x],[y,y],'b--')

    ax.plot([0,x],[0,y],'r')

    

    ax.set_xlim(-xmax,xmax)

    ax.set_ylim(-ymax,ymax)

    ax.set_aspect('equal')

    ax.grid(False)

    ax.set_xticks([])

    ax.set_yticks([])

    return

 

 

def update(startAngle, Hamp, Vamp, hOff, ax1,ax2):

    ax1.clear()

    ax2.clear()

    ax1 = drawPropagatingWave(ax1,off = startAngle, Hoffset = hOff, Ha = Hamp, Va = Vamp)  

    ax2 = drawPhaseDiagram(ax2,max = 4*np.pi, offset = startAngle, Hoffset = hOff,

                           Ha = Hamp, Va = Vamp)

 

    return ax1, ax2,

 

fig = plt.figure(figsize=(12,5))

 

gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1])

 

ax1 = plt.subplot(gs[0],projection='3d')

ax2 = plt.subplot(gs[1])

 

hOff = 2*np.pi/4.0

Ha = 1.0

Va = 1.0

ani = animation.FuncAnimation(fig, update,frames=np.linspace(0, 2*np.pi, 64),

                              fargs=(Ha,Va,hOff,ax1,ax2))

 

plt.show()