如何为按时间顺序排列的 Matplotlib 图制作动画?
matplotlibpythondata visualization更新于 2025/6/8 15:08:17
要为按时间顺序排列的 Matplotlib 图制作动画,我们可以采取以下步骤 −
- 设置图形大小并调整子图之间和周围的填充。
- 创建新图形或激活现有图形。
- 将轴添加到图形作为子图排列的一部分。
- 使用 after() 方法返回给定日期时间实例之后的第一个重复。
- 编写 animate() 方法来制作动画。将数据显示为图像,即在 2D 常规栅格上。
- 要显示图形,请使用 show() 方法。
示例
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111)
def animate():
data = np.random.randn(5, 5)
im = plt.imshow(data, cmap="plasma")
for i in np.arange(1, 100):
data = np.random.randn(5, 5)
im.set_data(data)
fig.canvas.draw()
win = fig.canvas.manager.window
fig.canvas.manager.window.after(10, animate)
plt.show()
输出

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

