Python Matplotlib使用Matplotlib中的FuncAnimation命令为数据帧中的数据设置动画
我有以数据帧格式保存的数据(xarray,类似于Pandas),我希望它使用pcolormesh设置动画Python Matplotlib使用Matplotlib中的FuncAnimation命令为数据帧中的数据设置动画,python,numpy,animation,matplotlib,xarray,Python,Numpy,Animation,Matplotlib,Xarray,我有以数据帧格式保存的数据(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 = mytes
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])将输出一个稍有不同的四元网格……等等
谢谢你的帮助 的签名是FuncAnimation(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()