如何使用matplotlib.animate在Python中制作动画等高线图?

如何使用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()
Python

输出

如何使用matplotlib.animate在Python中制作动画等高线图?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册