Reputation: 571
I want to show the elapsed time in my animation in matplotlib. I created a text instance, but when I am trying to update it (based on the frame number) nothing changes. Here is part of the code:
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()
The time depends on the frame number, so I tried this:
time_text.set_text('time = %.1d' % i)
but it doesn't get updated(stays "hello").
Any ideas? What am I doing wrong?
Upvotes: 1
Views: 4139
Reputation: 87496
Change this:
return lines + pts
return time_text
to this:
return lines + pts + [time_txt,]
The second return is never getting hit, so it doesn't know to update that artist.
Upvotes: 2