Plot animated text on the plot in Matplotlib

To animate text in a plot, we can take the following steps

  • Set the figure size and adjust the padding between and around the subplots.
  • Set x and y axis limit.
  • Initialize a variable, string.
  • Use text() method to place text over the plot.
  • Use FuncAnimation() to animate the text. Set text on the text axis.
  • Turn off the axes.
  • To display the figure, use show() method.


from matplotlib import pyplot as plt, animation
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots()
ax.set(xlim=(-1, 1), ylim=(-1, 1))
string = 'Hello, how are you doing?'
label = ax.text(0, 0, string[0], ha='center', va='center', fontsize=20, color="Red")

def animate(i):
   label.set_text(string[:i + 1])

anim = animation.FuncAnimation(
   fig, animate, interval=200, frames=len(string))


Updated on: 16-Jun-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started