我正在尝试使用matplotlib对函数进行动画处理:
但出于某些原因,当我在动画函数中像这样编写它时:
l = 0.5
k = (2 * np.pi)/l
f = 4
w = 2 * np.pi * f
y = np.sin(k * x + w * i)
它没有动画,只有当我用它动画的数字来写它的时候:
y = np.sin(15*x - 62 * i)
原因何在?为什么我不能使用另一种形式?
相关代码:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)
# initialization function
def init():
line.set_data([], [])
return line,
# animation function
def animate(i):
x = np.linspace(0, 2, 1000)
l = 0.5
k = (2 * np.pi)/l
f = 4
w = 2 * np.pi * f
y = np.sin(k * x + w * i)
#y = np.sin(15*x - 62 * i)
line.set_data(x, y)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=300, interval=20, blit=True)
anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
plt.show()
谢谢!!
发布于 2018-03-15 07:38:02
唯一的问题是您的f
是一个整数,因此w
是2 * np.pi
的倍数。对于整数i
,该函数基本上保持不变。请将f
更改为非整数。
https://stackoverflow.com/questions/49293067
复制相似问题