matplotlib 动画线图保持为空

我正在尝试遵循位于此处的基本动画教程并对其进行调整以显示已计算的数据集,而不是每帧评估一个函数,但我卡住了。我的数据集随着时间的推移涉及 XY 坐标,包含在列表中satxpossatypos我正在尝试创建一个动画,以便它跟踪从数据集开头到结尾的一条线,每 0.1 秒显示 1 个新点。对我出错的地方有什么帮助吗?

from matplotlib import pyplot as plt
from matplotlib import animation
import numpy as np

Code here creates satxpos and satypos as lists

fig = plt.figure()
ax = plt.axes(xlim=(-1e7,1e7), ylim = (-1e7,1e7))
line, = ax.plot([], [], lw=2)

def init():
    line.set_data([], [])
    return line,

def animate(i):
    line.set_data(satxpos[i], satypos[i])
    return line,

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

编辑:代码运行没有错误,但生成一个空白的绘图窗口,没有显示点/线,也没有任何动画。数据集生成正确并且在静态图中可以很好地查看。

存在的重要性欧内斯特

为了“跟踪从数据集开头到结尾的一行”,您需要对数组进行索引以在每个时间步长中多包含一个元素:

line.set_data(satxpos[:i], satypos[:i])

(注意:!)

代码中的其他所有内容看起来都很好,因此通过上述操作,您应该得到并扩展线图。然后您可能希望设置interval为大于 1 的值,因为这意味着 1 毫秒的时间步长(这可能有点太快了)。我想使用interval = 40可能是一个好的开始。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章