0

我有以数据帧格式(xarray,类似于 Pandas)保存的数据,我希望用 pcolormesh 对其进行动画处理。

import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

def animate(i):
    graph_data = mytest.TMP_P0_L1_GLL0[i]
    ax1.pcolormesh(graph_data)

FuncAnimation(plt,animate,frames=100)

由于某种原因它不起作用(没有错误,但是当我显示 fig 时它没有动画)。

数据的布局方式是 pcolormesh(mytest.TMP_P0_L1_GLL0[0]) 将输出一个四边形, pcolormesh(mytest.TMP_P0_L1_GLL0[1]) 将输出一个略有不同的四边形......等等

谢谢你的帮助!

4

1 回答 1

1

的签名FuncAnimationFuncAnimation(fig, func, ...)。而不是 pyplot 模块,您需要提供图形作为第一个参数进行动画处理。

此外,您需要保留对动画类的引用,ani = FuncAnimation. 以下是一个可以正常工作的最小示例。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

class test():
    TMP_P0_L1_GLL0 = [np.random.rand(5,5) for i in range(100)]

mytest = test()

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

def animate(i):
    graph_data = mytest.TMP_P0_L1_GLL0[i]
    ax1.pcolormesh(graph_data)

ani = FuncAnimation(fig,animate,frames=100)

plt.show()
于 2017-08-23T08:50:24.207 回答