python制作动画为什么不动_python - 为什么我的wave函数的简单动画代码不起作用? - SO中文参考 - www.soinside.com...

为什么我的wave函数的简单动画代码不起作用?

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

我试图动画原子中电子的波函数。我在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 = 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()

python

matplotlib

animation

2个回答

0

投票

问题出在您的数据上。首先,您必须使用单个数字调用set_data。其次,如果将能量函数除以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()

0

投票

问题是您要设置动画的数据介于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()

热门问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值