如何使用动画更新 Matplotlib 中的绘图标题?
要使用动画更新 Matplotlib 中的绘图标题,我们可以采取以下步骤 −
- 设置图形尺寸并调整子图之间和周围的填充。
- 使用 figure() 方法创建新图形或激活现有图形。
- 使用 numpy 创建 x 和 y 数据点。
- 获取当前坐标轴。
- 使用 text() 方法向坐标轴添加文本。
- 添加可用于通过重复调用函数创建动画的 animate 方法。
- 要显示图形,请使用 show() 方法。
示例代码
import numpy as np
from matplotlib import pyplot as plt, animation
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
x = np.linspace(-10, 10, 1000)
y = np.sin(x)
plt.plot(x, y)
ax = plt.gca()
ax.text(0.5, 1.100, "y=sin(x)", bbox={'facecolor': 'red',
'alpha': 0.5, 'pad': 5},
transform=ax.transAxes, ha="center")
def animate(frame):
ax.text(0.5, 1.100, "y=sin(x), frame: %d" % frame,
bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 5},
transform=ax.transAxes, ha="center")
ani = animation.FuncAnimation(fig, animate, interval=100,
frames=10, repeat=True)
plt.show()输出

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