如何在Matplotlib中实现文本动画?

如何在Matplotlib中实现文本动画?

在Matplotlib中实现文本动画,可以按照以下步骤进行−

  • 从Matplotlib中导入“animation”包。
  • 设置图形大小并调整子图之间和周围的填充。
  • 创建一个新的图形或激活现有图形。
  • 将“ax”添加到图形中,作为子图排列的一部分。
  • 初始化一个变量“text”来存储一个字符串。
  • x=0.20y=0.50 的轴上添加文本。
  • 制作一个颜色列表。
  • 通过反复调用函数animate来制作动画,其中文本的大小增加并更改颜色。
  • 使用 show() 方法显示图形。

例子

from matplotlib import animation
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

fig = plt.figure()
ax = fig.add_subplot(111)
text = '欢迎您!'
txt = ax.text(.20, .5, text, fontsize=15)

colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2',    '#7f7f7f', '#bcbd22', '#17becf']


def animate(num):
   txt.set_fontsize(num * 2 + num)
   txt.set_color(colors[num % len(colors)])
   return txt,

anim = animation.FuncAnimation(fig, animate, frames=len(text) - 1, blit=True)

plt.show()

输出

它将生成以下输出

如何在Matplotlib中实现文本动画?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程