matplotlib动画不会为正弦函数设置动画

时间:2018-03-15 06:43:28

标签: python animation matplotlib

我正在尝试使用matplotlib为该函数设置动画:

enter image description here

但出于某种原因,当我在animate函数中编写它时:

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

谢谢!

1 个答案:

答案 0 :(得分:0)

唯一的问题是,您的f是一个整数,因此w2 * np.pi的倍数。整数i的函数基本不变。将f更改为非整数。

相关问题