在 matplotlib 中显示经过的时间(帧编号)

问题描述 投票:0回答:1

我想在 matplotlib 中显示动画中经过的时间。我创建了一个文本实例,但是当我尝试更新它(基于帧号)时,没有任何变化。这是部分代码:

fig = plt.figure()
ax = plt.axes(xlim =(-4E8,4E8), ylim= (-4E8,4E8))
time_text = ax.text(0.05, 0.95,'',horizontalalignment='left',verticalalignment='top', transform=ax.transAxes)

def init():
    for line, pt in zip(lines, pts):
        line.set_data([], [])

        pt.set_data([], [])
        time_text.set_text('hello')
    return lines + pts
    return time_text

def animate(i):
    i = (10 * i) % data.shape[1]
    #update lines and points here
    for line, pt, dt in zip(lines,pts, data):
        x, y, z = dt[:i].T
        line.set_data(x, y)
        
        pt.set_data(x[-1:], y[-1:])

        time_text.set_text('time = %.1d' % i) #<<<<<Here. This doesn't work
    return lines + pts
    return time_text

anim = animation.FuncAnimation(fig, animate, init_func=init,
                           frames=700, interval=1, blit=True)
plt.show()

时间取决于帧数,所以我尝试了这个:

time_text.set_text('time = %.1d' % i)

但它没有更新(保持“你好”)。

有什么想法吗?我做错了什么?

python animation matplotlib
1个回答
2
投票

更改此:

return lines + pts
return time_text

对此:

return lines + pts + [time_txt,]

第二个回报永远不会被击中,所以它不知道更新那个艺术家。

© www.soinside.com 2019 - 2024. All rights reserved.