Abh*_*hek 3 python animation matplotlib
我正在尝试绘制一条线,并且它的三个点位于两个列表中:x,y。该代码有效,但我看不到在我面前渲染的线,因此,它看起来一直像图像。如何减慢这个动画的速度?这是代码:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
fig = plt.figure()
ax = plt.axes(xlim=(0, 105), ylim=(0, 68))
line, = ax.plot([], [], lw=2)
def init():
line.set_data([], [])
return line,
def animate(i):
x = np.array([23.94, 34.65, 28.14])
y = np.array([5.984, 6.664, 6.256])
#x = np.linspace(0, 2, 1000)
#y = np.sin(2 * np.pi * (x - 0.01 * i))
line.set_data(x, y)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=1, interval=1, save_count = 50, blit=True)
FFWriter = animation.FFMpegWriter()
#ani.save('particle_box.avi', writer = FFWriter)
#anim.save('basic.mp4', writer = FFWriter)#, fps=30)#, extra_args=['-vcodec', 'libx264'])
plt.show()
Run Code Online (Sandbox Code Playgroud)
好吧,首先,您的动画只有两种有效状态,因为动画函数实际上没有执行任何操作。
您可以分解x和的定义y,因为它们实际上并没有改变。要实际执行动画,您应该做的是使线上的点随着每次调用而改变,这可以通过切片和 来anim_func完成: xy
x = np.array([23.94, 34.65, 28.14])
y = np.array([5.984, 6.664, 6.256])
def animate(i):
line.set_data(x[:i], y[:i])
Run Code Online (Sandbox Code Playgroud)
最后,您应该修改您的FuncAnimation创建以具有更长的间隔,例如:
anim = animation.FuncAnimation(fig, animate, init_func=init,
interval=1000, save_count = 50, blit=True)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4961 次 |
| 最近记录: |