Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Example
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))
ax.axis('off')
plt.show()
Output

Advertisements