在 Matplotlib 绘图上绘制动画文本
要为绘图中的文本添加动画效果,我们可以执行以下步骤:
- 设定图片大小和调整子图之间的边距。
- 设定 x 及 y 轴的限制条件。
- 初始化一个变量,字符串。
- 使用 text() 函数在绘图上放置文本。
- 使用 FuncAnimation() 函数为文本添加动画。将文本设定在文本轴。
- 关闭轴。
- 要显示图片,请使用 show() 函数。
示例
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()输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP