matplotlib 中带有轮廓的动画

要在 matplotlib 中使用轮廓制作动画,我们可以采取以下步骤

脚步

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

  • 为等高线图创建数据。

  • 创建一个图形和一组子图。

  • 通过重复调用函数*animate*来生成动画,其中该animate()方法会更改轮廓数据点。

  • 要显示图形,请使用Show()方法。

示例

import numpy as np
importmatplotlib.pyplotas plt
importmatplotlib.animationas animation

# Set the figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Random data for the contour plot
data = np.random.randn(800).reshape(10, 10, 8)

# Create a figure and a set of subplots
fig, ax = plt.subplots()

# Method to change the contour data points
def animate(i):
    ax.clear()
    ax.contourf(data[:, :, i], cmap='plasma')

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

# Display the plot
plt.show()
输出结果

它将产生以下输出 -