如何使用matplotlib.animate在Python中制作动画等高线图?
要在Python的matplotlib中制作动画等高线图,可以按以下步骤进行:
- 创建一个10☓10维度的随机数据。
- 使用 subplots() 方法创建一个图和一组子图。
- 通过重复调用函数 func ,使用 FuncAnimation() 类制作动画。
- 要在函数中更新等高线值,我们可以定义一个方法animate,可以在 FuncAnimation() 类中使用。
- 使用 show() 方法来显示图。
示例
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
plt.rcParams["figure.figsize"] = [7.00, 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()