使用Matplotlib对氢原子进行3D动画制作。

问题描述 投票:1回答:1

我想制作一个氢原子轨道的3D动画。因此,我创建了以下程序。

#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()

现在我想做一个动画,当时间t从0到2*pi的时候,对象是如何变化的。我如何使用matplotlib来完成这个任务?我试着在教程的帮助下做这个,但是很困惑。谢谢大家的支持。

PS:如果有人哪怕有一个想法,如何用blender渲染这个......你将是我的英雄。

python animation physics blender
1个回答
2
投票

这是很直接的使用 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))

您可以根据需要调整帧数。interval 指定帧与帧之间的间隔,单位是毫秒--我在这里将其缩放,所以动画总是2秒长。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.