如何在 Matplotlib 中对矢量网格图进行动画处理?


若要在 matplotlib 中对pcolormesh 进行动画处理,我们可以采取以下步骤 -

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

  • 使用 numpy 创建 x、y 和 t 数据点。

  • 创建 X3、Y3 和 T3,从坐标向量返回坐标矩阵,方法使用网格。

  • 使用 pcolormesh() 方法创建一个具有非规则矩形栅格的伪彩色图。

  • 使用 colormesh 颜色轴制作一个颜色条。

  • 使用 Animation() 类方法对 pcolormesh 进行动画处理。

  • 若要显示图表,请使用 show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt, animation
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

fig, ax = plt.subplots()
x = np.linspace(-3, 3, 91)
t = np.linspace(0, 25, 30)
y = np.linspace(-3, 3, 91)
X3, Y3, T3 = np.meshgrid(x, y, t)
sinT3 = np.sin(2 * np.pi * T3 / T3.max(axis=2)[..., np.newaxis])
G = (X3 ** 2 + Y3 ** 2) * sinT3
cax = ax.pcolormesh(x, y, G[:-1, :-1, 0], vmin=-1, vmax=1, cmap='Blues')
fig.colorbar(cax)

def animate(i):
   cax.set_array(G[:-1, :-1, i].flatten())

anim = animation.FuncAnimation(fig, animate, interval=100, frames=len(t) - 1)
anim.save('517.gif')
plt.show()

输出

更新日期: 2021 年 5 月 11 日

3000+ 浏览

开启你的 职业生涯

完成课程以获得认证

开始
广告
© . All rights reserved.