如何在 Matplotlib 中为 pcolormesh 制作动画?
matplotlibpythondata visualization
要在 matplotlib 中为 pcolormesh 制作动画,我们可以采取以下步骤 −
创建一个图形和一组子图。
使用 numpy 创建 x、y 和 t 数据点。
使用 meshgrid 创建 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()