Skip to main content
Chemistry LibreTexts

Spherical Harmonics Visualization (Python Notebook)

  • Page ID
    283935
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Source: https://github.com/DalInar/schroding...lization.ipynb

    The Legendre Polynomials

    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.special import lpmv
    
    ls = [0,1,2,3]
    x=np.linspace(-1,1,100)
    
    plt.figure()
    
    for l in ls:
        plt.plot(x,lpmv(0,l,x),label=r'$l=$'+str(l))
    plt.title(r'Legendre Polynomials, $P_l(x)$')
    plt.xlabel(r'$x$')
    plt.ylabel(r'$P_l(x)$')
    plt.legend()
    plt.grid()
    plt.show()

    The Legendre Polynomials on a polar plot

    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.special import lpmv
    
    ls = [0,1,2,3]
    
    thetas = np.linspace(0,2*np.pi,200)
    
    plt.figure(figsize=(8,8))
    for i in range(0, len(ls)):
        l=ls[i]
        r = lpmv(0,l,np.cos(thetas))
        plt.polar(thetas, abs(r),label=r'$l=$'+str(l))
    plt.title(r'Associated Legendre Polynomials, $||P_l(x)||$')
    plt.ylabel(r'$||P_l(x)||$')
    plt.legend()
    plt.show()

    The Associated Polynomials  

    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.special import lpmv
    
    ls = [0,1,1,1,2]
    ms = [0,-1,0,1,1]
    
    x=np.linspace(-1,1,100)
    
    plt.figure()
    
    for i in range(0, len(ls)):
        l=ls[i]
        m=ms[i]
        plt.plot(x,lpmv(m,l,x),label=r'$l=$'+str(l))
    plt.title(r'Associated Legendre Polynomials, $P_l^m(x)$')
    plt.xlabel(r'$x$')
    plt.ylabel(r'$P_l^m(x)$')
    plt.legend()
    plt.show()

     The Associated Polynomials on a Polar plot

    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.special import lpmv
    
    ls = [0,1,1,1,2]
    ms = [0,-1,0,1,1]
    thetas = np.linspace(0,2*np.pi,200)
    
    plt.figure(figsize=(8,8))
    for i in range(0, len(ls)):
        l=ls[i]
        m=ms[i]
        r = lpmv(m,l,np.cos(thetas))
        plt.polar(thetas, abs(r),label=r'$l=$'+str(l)+r', $m=$'+str(m))
    plt.title(r'Associated Legendre Polynomials, $||P_l^m(x)||$')
    plt.ylabel(r'$||P_l^m(x)||$')
    plt.legend()
    plt.show()

    The Associated Polynomials in 3D 

    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.special import sph_harm
    import mpl_toolkits.mplot3d.axes3d as axes3d
    import matplotlib.colors as mcolors
    
    l=2
    m=1
    
    thetas = np.linspace(0, np.pi, 20)
    phis = np.linspace(0, 2*np.pi, 20)
    
    (Theta,Phi)=np.meshgrid(thetas,phis) 
    s_harm=sph_harm(m, l, Phi, Theta)
       
    R = abs(s_harm)
    X = R * np.sin(Theta) * np.cos(Phi)
    Y = R * np.sin(Theta) * np.sin(Phi)
    Z = R * np.cos(Theta)
    
    cmap = plt.get_cmap('jet')
    norm = mcolors.Normalize(vmin=Z.min(), vmax=Z.max())
    
    fig = plt.figure(figsize=(8,8))
    ax = fig.add_subplot(1,1,1, projection='3d')
    ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('jet'),facecolors=cmap(norm(R)),
        linewidth=0, antialiased=False, alpha=0.5)
    plt.title(r'Spherical Harmonics, $Y_l^m(\theta,\phi)$'+r', $l=$'+str(l)+r', $m=$'+str(m))
    plt.xlabel(r'$x$')
    plt.ylabel(r'$y$')
    plt.ylabel(r'$z$')
    
    plt.show()

     


    Spherical Harmonics Visualization (Python Notebook) is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?