简体   繁体   English

使用氢原子的 3D Animation Matplotlib

[英]3D Animation of hydrogen atom using Matplotlib

I want to create a 3D Animation of the orbitals of an hydrogen atom.我想创建一个氢原子轨道的 3D Animation。 Therefore I created the following programm:因此我创建了以下程序:

#Repositorys
import numpy as np
from scipy.special import sph_harm
import matplotlib.pyplot as plt
import matplotlib as matplotlib
from mpl_toolkits.mplot3d import Axes3D
import cmath


#Create Diagramm
fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(111, projection='3d')

#Variables
l = 0
m = 0
phi = np.linspace(0, np.pi , 150)
theta = phi = np.linspace(0, 2*np.pi , 150)

#Variables for linear combination
l2 = 1
m2 = 0
t = 0

#Calculate  linear combination
X = abs(sph_harm(m, l, theta, phi)  + sph_harm(m2, l2, theta, phi) * cmath.exp(-t*1j))  * np.outer(np.cos(phi), np.sin(theta))
Y = abs(sph_harm(m, l, theta, phi)  + sph_harm(m2, l2, theta, phi) * cmath.exp(-t*1j)) * np.outer(np.sin(phi), np.sin(theta))
Z = abs(sph_harm(m, l, theta, phi)  + sph_harm(m2, l2, theta, phi) * cmath.exp(-t*1j)) * np.outer(np.ones(np.size(phi)), np.cos(theta))

ax.plot_surface(X, Y, Z, rstride=4, cstride=4, color='b')

plt.show()

Now I wanted to animate, how the object changes when the time t runs from 0 to 2*pi.现在我想制作动画,当时间 t 从 0 运行到 2*pi 时 object 如何变化。 How can I do this using matplotlib?如何使用 matplotlib 做到这一点? I tried to do this with the help of tutorials but got confused.我试图在教程的帮助下做到这一点,但感到困惑。 Thank you for your support.谢谢您的支持。

PS: If someone even has an idea how to render this with blender...You would be my hero PS:如果有人甚至知道如何用搅拌机渲染这个......你会是我的英雄

This is pretty straightforward using matplotlib.animation.FuncAnimation -这很简单,使用matplotlib.animation.FuncAnimation -

import numpy as np
from scipy.special import sph_harm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation
import cmath

fig = plt.figure(figsize = (7,7))
ax = fig.add_subplot(111, projection='3d')

l = 0
m = 0
l2 = 1
m2 = 0
phi = np.linspace(0, np.pi , 150)
theta = phi = np.linspace(0, 2*np.pi , 150)

surf = ax.plot_surface(np.array([[]]), np.array([[]]), np.array([[]]))
ax.set_xlim([-0.75, 0.75])
ax.set_ylim([-0.75, 0.75])
ax.set_zlim([-0.75, 0.75])

def animate(i):
    global surf
    t = 2 * np.pi / nframes * i;
    X = abs(sph_harm(m, l, theta, phi)  + sph_harm(m2, l2, theta, phi) * cmath.exp(-t*1j)) \  
        * np.outer(np.cos(phi), np.sin(theta))
    Y = abs(sph_harm(m, l, theta, phi)  + sph_harm(m2, l2, theta, phi) * cmath.exp(-t*1j)) \
        * np.outer(np.sin(phi), np.sin(theta))
    Z = abs(sph_harm(m, l, theta, phi)  + sph_harm(m2, l2, theta, phi) * cmath.exp(-t*1j)) \
        * np.outer(np.ones(np.size(phi)), np.cos(theta))

    surf.remove()
    fig.canvas.draw()
    surf = ax.plot_surface(X, Y, Z, rstride=4, cstride=4, color='b')

nframes = 36
anim = FuncAnimation(fig, animate, frames=nframes+1, interval=2000/(nframes+1))

You can scale the number of frames as needed, interval specifies the interval between frames in milliseconds - I have it scaled here so the animation is always 2 seconds long:您可以根据需要缩放帧数, interval以毫秒为单位指定帧之间的间隔 - 我在这里对其进行了缩放,因此 animation 始终为 2 秒长:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM