在 Matplotlib 中绘制动画文本
matplotlibpythondata visualization更新于 2025/7/10 22:36:17
要为图中的文本添加动画效果,我们可以执行以下步骤
- 设置图形大小并调整子图之间和周围的填充。
- 设置 x 和 y 轴限制。
- 初始化变量 string。
- 使用 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()
输出

相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

