Matplotlib 动画在 IPython Notebook 中不起作用?

matplotlibpythondata visualization

要在 matplotlib 中为图表制作动画,我们可以采取以下步骤 −

  • 设置图形大小并调整子图之间和周围的填充。

  • 创建形状为 10X10 维度的随机数据。

  • 使用 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()

输出


相关文章