如何在 Matplotlib 中将参数传递给 animation.FuncAnimation()?

pythonserver side programmingprogramming更新于 2025/8/8 15:08:17

要将参数传递给 Python 中 Matplotlib 中轮廓图的 animation.FuncAnimation(),我们可以采取以下步骤 −

  • 创建一个 10☓10 维的随机数据。
  • 使用 subplots() 方法创建一个图形和一组子图。
  • 通过使用 FuncAnimation() 类重复调用函数 *func* 来制作动画
  • 要更新函数中的轮廓值,我们可以定义一个可在 FuncAnimation() 类中使用的方法 animate()。
  • 要显示图形,请使用 show() 方法。

示例

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

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

data = np.random.randn(800).reshape(10, 10, 8)
fig, ax = plt.subplots()

def animate(i):
   ax.clear()
   ax.contourf(data[:, :, i])

ani = animation.FuncAnimation(fig, animate, 5, interval=50, blit=False)

plt.show()

输出


相关文章


有用资源