Python 为什么我的波函数的简单动画代码不起作用?
我正试图使原子中电子的波函数生动起来。我按照Matplotlob动画文档中给出的内容编写了最简单的python代码,但它没有做任何事情。有人能帮忙吗Python 为什么我的波函数的简单动画代码不起作用?,python,matplotlib,animation,Python,Matplotlib,Animation,我正试图使原子中电子的波函数生动起来。我按照Matplotlob动画文档中给出的内容编写了最简单的python代码,但它没有做任何事情。有人能帮忙吗 import matplotlib.pyplot as plt from matplotlib import animation import numpy as np import math angles = (np.linspace(0, 2 * np.pi, 360, endpoint=False)) fig= plt.figure() ax
import matplotlib.pyplot as plt
from matplotlib import animation
import numpy as np
import math
angles = (np.linspace(0, 2 * np.pi, 360, endpoint=False))
fig= plt.figure()
ax = fig.add_subplot(111, polar=True)
line1, =ax.plot([],[], 'g-', linewidth=1)
def update(theta):
line1.set_data(angles,energy_band(3, theta, 3))
return line1,
def init():
line1.set_data([],[])
return line1,
def energy_band(wave_number, phase_offset, energy_level):
return [math.sin(2*np.pi/360*i*wave_number+phase_offset*np.pi/360)+energy_level for i in range(360)]
ani = animation.FuncAnimation(fig, update, frames=[i for i in range(0,3600,5)], blit=True, interval=200, init_func=init)
plt.show()
问题在于你的数据。首先,必须使用单个数字调用set_数据。第二,若你们把能量函数除以100,你们就得到了一个很好的比例来显示数据。此外,我设定了轴的极限。检查我如何修改您的代码:
line1, =ax.plot([],[], 'ro')# 'g-', linewidth=1)
def update(theta):
line1.set_data(angles[int(theta)], energy_band(3, theta, 3)[int(theta)]/100)
#line1.set_data(angles,energy_band(3, theta, 3)/100)
return line1,
def init():
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1, 1)
#line1.set_data([],[])
return line1,
另一件事是互动模式。当matplotlib什么都不做时,这通常是一个问题,尤其是在使用jupyter笔记本时
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
matplotlib.interactive(True)
plt.ion()
matplotlib.is_interactive()
问题在于你的数据。首先,必须使用单个数字调用set_数据。第二,若你们把能量函数除以100,你们就得到了一个很好的比例来显示数据。此外,我设定了轴的极限。检查我如何修改您的代码:
line1, =ax.plot([],[], 'ro')# 'g-', linewidth=1)
def update(theta):
line1.set_data(angles[int(theta)], energy_band(3, theta, 3)[int(theta)]/100)
#line1.set_data(angles,energy_band(3, theta, 3)/100)
return line1,
def init():
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1, 1)
#line1.set_data([],[])
return line1,
另一件事是互动模式。当matplotlib什么都不做时,这通常是一个问题,尤其是在使用jupyter笔记本时
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
matplotlib.interactive(True)
plt.ion()
matplotlib.is_interactive()
问题在于,要设置动画的数据位于2和4之间,但极坐标图仅显示-0.04和0.04之间的范围。这是因为在开始时使用了空绘图。这将需要您手动设置限制。比如说,
ax.set_rlim(0,5)
这是代码工作所需的唯一添加项
但是,可以进行更多的优化,例如在整个过程中使用numpy并重用现有变量
import matplotlib.pyplot as plt
from matplotlib import animation
import numpy as np
angles = (np.linspace(0, 2 * np.pi, 360, endpoint=False))
fig= plt.figure()
ax = fig.add_subplot(111, polar=True)
line1, =ax.plot([],[], 'g-', linewidth=1)
def update(theta):
line1.set_data(angles,energy_band(3, theta, 3))
return line1,
def init():
line1.set_data([],[])
ax.set_rlim(0,5)
return line1,
def energy_band(wave_number, phase_offset, energy_level):
return np.sin(angles*wave_number+phase_offset*np.pi/360)+energy_level
ani = animation.FuncAnimation(fig, update, frames=np.arange(0,3600,5),
blit=True, interval=200, init_func=init)
plt.show()
问题在于,要设置动画的数据位于2和4之间,但极坐标图仅显示-0.04和0.04之间的范围。这是因为在开始时使用了空绘图。这将需要您手动设置限制。比如说,
ax.set_rlim(0,5)
这是代码工作所需的唯一添加项
但是,可以进行更多的优化,例如在整个过程中使用numpy并重用现有变量
import matplotlib.pyplot as plt
from matplotlib import animation
import numpy as np
angles = (np.linspace(0, 2 * np.pi, 360, endpoint=False))
fig= plt.figure()
ax = fig.add_subplot(111, polar=True)
line1, =ax.plot([],[], 'g-', linewidth=1)
def update(theta):
line1.set_data(angles,energy_band(3, theta, 3))
return line1,
def init():
line1.set_data([],[])
ax.set_rlim(0,5)
return line1,
def energy_band(wave_number, phase_offset, energy_level):
return np.sin(angles*wave_number+phase_offset*np.pi/360)+energy_level
ani = animation.FuncAnimation(fig, update, frames=np.arange(0,3600,5),
blit=True, interval=200, init_func=init)
plt.show()
您好,请提供您的代码,以便我们能够找出哪里出了问题。刚刚在问题中添加了相关代码。@Buzz您希望结果是什么样子?您好,请提供您的代码,以便我们能够找出哪里出了问题。刚刚在问题中添加了相关代码。@Buzz您希望结果是什么样子?