Skip to main content
Chemistry LibreTexts

Hydrogen Orbitals (Python Notebook)

  • Page ID
    281075
  • \( \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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

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

    (Notes: Different version of scikit-image is used. The right version should be https://pypi.org/project/scikit-image/0.12.3/#files)

    import skimage
    print(skimage.__version__)
    import numpy
    import math
    import matplotlib.pyplot as plt
    from matplotlib.widgets import Slider, Button, RadioButtons
    import scipy.special
    from scipy.special import sph_harm
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
    from matplotlib.colors import ListedColormap
    import skimage
    
    def hydrogen_wf(n,l,m,X,Y,Z):
        R = numpy.sqrt(X**2+Y**2+Z**2)
        Theta = numpy.arccos(Z/R)
        Phi = numpy.arctan2(Y,X)
        
        rho = 2.*R/n
        s_harm=sph_harm(m, l, Phi, Theta)
        l_poly = scipy.special.genlaguerre(n-l-1,2*l+1)(rho)
        
        prefactor = numpy.sqrt((2./n)**3*math.factorial(n-l-1)/(2.*n*math.factorial(n+l)))
        wf = prefactor*numpy.exp(-rho/2.)*rho**l*s_harm*l_poly
        wf = numpy.nan_to_num(wf)
        return wf
      
    dz=0.5
    zmin=-10
    zmax=10
    x = numpy.arange(zmin,zmax,dz)
    y = numpy.arange(zmin,zmax,dz)
    z = numpy.arange(zmin,zmax,dz)
    X,Y,Z = numpy.meshgrid(x,y,z) #X, Y, Z are 3d arrays that tell us the values of x, y, and z at every point in space
    
    #Change these to change which orbital to plot
    n=4
    l=2
    m=0
    
    data = hydrogen_wf(n,l,m,X,Y,Z)
    data = abs(data)**2
    
    R = numpy.sqrt(X**2+Y**2+Z**2)
    
    fig, ax = plt.subplots()
    plt.subplots_adjust(left=0.15, bottom=0.15)
    im = plt.imshow(data[int((0-zmin)/dz),:,:], vmin=0, vmax = numpy.max(data), extent=[zmin,zmax,zmin,zmax])
    plt.colorbar()
    sli = Slider(plt.axes([0.25, 0.025, 0.65, 0.03]), "Y", z[0], z[len(z)-1], valinit=0)
    ax.set_title("Hydrogen Orbital xz Slice (y="+str("%.2f"%sli.val)+"): n="+str(n)+", l="+str(l)+", m="+str(m))
    
    def update(val):
        index = int((sli.val-zmin) / dz)
        im.set_data(data[index,:,:])
        ax.set_title("Hydrogen Orbital xz Slice (y="+str("%.2f"%sli.val)+"): n="+str(n)+", l="+str(l)+", m="+str(m))
           
    sli.on_changed(update)
    plt.show()
    import numpy
    import math
    import matplotlib.pyplot as plt
    from matplotlib.widgets import Slider, Button, RadioButtons
    import scipy.special
    from scipy.special import sph_harm
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
    from matplotlib.colors import ListedColormap
    import skimage
    from skimage import measure
    
    def hydrogen_wf(n,l,m,X,Y,Z):
        R = numpy.sqrt(X**2+Y**2+Z**2)
        Theta = numpy.arccos(Z/R)
        Phi = numpy.arctan2(Y,X)
        
        rho = 2.*R/n
        s_harm=sph_harm(m, l, Phi, Theta)
        l_poly = scipy.special.genlaguerre(n-l-1,2*l+1)(rho)
        
        prefactor = numpy.sqrt((2./n)**3*math.factorial(n-l-1)/(2.*n*math.factorial(n+l)))
        wf = prefactor*numpy.exp(-rho/2.)*rho**l*s_harm*l_poly
        wf = numpy.nan_to_num(wf)
        return wf
      
    dz=0.5
    zmin=-10
    zmax=10
    x = numpy.arange(zmin,zmax,dz)
    y = numpy.arange(zmin,zmax,dz)
    z = numpy.arange(zmin,zmax,dz)
    X,Y,Z = numpy.meshgrid(x,y,z) #X, Y, Z are 3d arrays that tell us the values of x, y, and z at every point in space
    n=4
    l=2
    m=0
    data = hydrogen_wf(n,l,m,X,Y,Z)
    data = abs(data)**2
    R = numpy.sqrt(X**2+Y**2+Z**2)
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.set_xlim([0,len(x)])
    ax.set_ylim([0,len(y)])
    ax.set_zlim([0,len(z)])
    max_val = numpy.max(data)
    
    verts, faces, _, _ = measure.marching_cubes(data, max_val/2, spacing = (1,1,1))
    result=ax.plot_trisurf(verts[:,0], verts[:,1], faces, verts[:,2], cmap ='Spectral', lw=0)
    
    sli = Slider(plt.axes([0.25, 0.025, 0.65, 0.03]), "iso", 0, max_val, valinit=max_val/2)
    ax.set_title("Hydrogen Orbital Isosurface ("+str("%.5f"%sli.val)+"): n="+str(n)+", l="+str(l)+", m="+str(m))
    
    def update(val):
        ax.clear()
        verts, faces = measure.marching_cubes(data, sli.val, spacing = (1,1,1))
        result = ax.plot_trisurf(verts[:,0], verts[:,1], faces, verts[:,2], cmap ='Spectral', lw=0)
        ax.set_xlim([0,len(x)])
        ax.set_ylim([0,len(y)])
        ax.set_zlim([0,len(z)])
        ax.set_title("Hydrogen Orbital Isosurface ("+str("%.5f"%sli.val)+"): n="+str(n)+", l="+str(l)+", m="+str(m))
               
    sli.on_changed(update)
    plt.show()
    import numpy
    import math
    import matplotlib.pyplot as plt
    import scipy.special
    from scipy.special import sph_harm
    
    def hydrogen_wf(n,l,m,X,Y,Z):
        R = numpy.sqrt(X**2+Y**2+Z**2)
        Theta = numpy.arccos(Z/R)
        Phi = numpy.arctan2(Y,X)
        
        rho = 2.*R/n
        s_harm=sph_harm(m, l, Phi, Theta)
        l_poly = scipy.special.genlaguerre(n-l-1,2*l+1)(rho)
        
        prefactor = numpy.sqrt((2./n)**3*math.factorial(n-l-1)/(2.*n*math.factorial(n+l)))
        wf = prefactor*numpy.exp(-rho/2.)*rho**l*s_harm*l_poly
        wf = numpy.nan_to_num(wf)
        return wf
      
    dz=0.5
    zmin=-10
    zmax=10
    x = numpy.arange(zmin,zmax,dz)
    y = numpy.arange(zmin,zmax,dz)
    z = numpy.arange(zmin,zmax,dz)
    X,Y,Z = numpy.meshgrid(x,y,z) #X, Y, Z are 3d arrays that tell us the values of x, y, and z at every point in space
    n=3
    l=1
    m=0
    data = hydrogen_wf(n,l,m,X,Y,Z)
    data = abs(data)**2
    R = numpy.sqrt(X**2+Y**2+Z**2)
    
    plt.figure()
    plt.plot(z, data[int(len(z)/2),int(len(z)/2),:])
    plt.title("$|\psi_{nlm}|^2$(x=0,y=0,z): n="+str(n)+", l="+str(l)+", m="+str(m))
    plt.xlabel('z')
    plt.ylabel("$|\psi_{nlm}|^2$")
    plt.show()

     


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

    • Was this article helpful?