gpt4 book ai didi

python - 如何使用 matplotlib.animation 在 python 中对数据图进行动画处理

转载 作者:太空宇宙 更新时间:2023-11-03 21:05:18 25 4
gpt4 key购买 nike

我有两个数组 x 和 y,每个数组都有超过 365000 个元素。我想使用这些数组元素绘制一条动画线。我正在使用 matplotlib.animation 。问题是当我执行下面的代码时,我看不到平滑(动画)绘制的图表。相反,我看到的是最终绘制的版本。

This is what I obtain

这是我的代码:

#libs
# Movement instance creation-----------------------------
movement1=Movement(train1, track1)
# # Move the train on the track

movement1.move()
y = movement1.speed
x = movement1.pos

Writer = animation.writers['ffmpeg']
writer = Writer(fps=20, metadata=dict(artist='Me'), bitrate=1800)

fig = plt.figure()
ax = plt.axes(xlim=(0, 25), ylim=(0, 300))
line, = ax.plot([], [], lw=2)
# initialization function: plot the background of each frame
def init():
line.set_data([], [])
return line,

# animation function. This is called sequentially
def animate(i):
line.set_data(x, y)
return line,

anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=200, blit=True)


anim.save('basic_animation.mp4', writer=writer)

这是我期望的类似结果:

Here is the similar result that I expect.

当然,我的图表将是另一条曲线。

最佳答案

你的代码大部分都很好;你只需要做三件事。

  1. 每次anim_func迭代时,将线条的xdataydata设置为不同的值(否则,不会有动画,会有吗?)

  2. 设置恒定的轴限制,这样绘图就不会改变形状

  3. 出于显示目的删除 save 调用(就我个人而言,我发现它会影响动画)

所以:

ax.axis((x.min(), x.max(), y.min(), y.max())

def animate(i):
line.set_data(x[:i], y[:i])
return line,

关于python - 如何使用 matplotlib.animation 在 python 中对数据图进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55448378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com