如何在Python中使用matplotlib.animate来对等值线图进行动画处理?


若要使用matplotlib 在Python中对等值线图进行动画处理,我们可以采取以下步骤-

  • 创建形状为10☓10维度的随机数据。
  • 使用subplots()方法创建一幅图和一组子图。
  • 使用FuncAnimation()类反复调用函数*func*,制作动画。
  • 若要更新函数中的等值线值,我们可以定义一个方法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()

输出

更新于:2021年5月15日

655次浏览

开启您的 职业生涯

完成课程,获得认证

开始学习
广告
© . All rights reserved.