14

I am trying to animate a text box in a Matplotlib figure, but can't seem to get it working. Does anyone know how to do this properly? An example is below.

from matplotlib import animation
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap

fig = plt.figure()
ax = fig.add_subplot(111)

times = ['first', 'second', 'third']

time_text = ax.text(.5, .5, '', fontsize=15)


def updatefig(num):
    global mt
    mt = ax.text(.5, .5, times[num], fontsize=15)

anim = animation.FuncAnimation(fig, updatefig, frames=len(times)-1, blit=True, init_func=init)
0

1 Answer 1

26

Text is an artist and you animate it exactly like any other artist:

def updatefig(num):
    time_text.set_text(times[num])
    return time_text,
Sign up to request clarification or add additional context in comments.

2 Comments

I know this is old, but how do you also update the location of the text on the graph?
I would assume you use something like time_text.set_x(x[num]) and time_text.set_y(y[num])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.