我想在 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)
但它没有得到更新(保持“你好”)。
有任何想法吗?我究竟做错了什么?