我想在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)
但是它不会被更新(保持"hello")。
任何想法?我做错了什么?
修改如下:
return lines + pts
return time_text
:
return lines + pts + [time_txt,]
第二次返回永远不会被击中,所以它不知道要更新那个artist