很抱歉,无论我使用多少示例,我都不能理解如何通过animation.FuncAnimation模块放入参数。我的任务很简单,我有地球物理数组(time,x,y)。我想要的就是让某个字段随时间变化的动画效果。我想我的函数参数应该是我的绘图函数,沿着时间轴改变索引。但它就是不会发生。
field.shape
(12,912,1125)
X,Y = np.meshgrid(lon,lat)
fig, ax = plt.subplots()
def animate(dset,i):
ax[i] = plt.pcolormesh(X,Y,field_monthly[i].T)
plt.colorbar()
plt.set_cmap('viridis')
return ax
i = np.arange(12)
anim = animation.FuncAnimation(fig, animate(field_monthly,i), frames=12,
interval=500,
repeat=False,
blit=False)我知道我的逻辑中有一些基本的漏洞,但找不到它。上面的代码是我尝试扭曲和转弯函数和索引的50种方法中的一种。谢谢!
发布于 2020-10-06 06:11:54
在你的实现中有一些问题。
fargs来传递附加参数中出现一次
解决这些问题:
from matplotlib import animation, pyplot as plt
import numpy as np
k, n, m = 12, 30, 50
field = np.random.random((k, n, m))
x, y = np.meshgrid(np.arange(n), np.arange(m))
fig, ax = plt.subplots()
plt.pcolormesh(x, y, field[0].T)
plt.colorbar()
plt.set_cmap('viridis')
def animate(i, field2):
plt.cla()
h = plt.pcolormesh(x, y, field[i].T)
return h,
anim = animation.FuncAnimation(fig=fig, func=animate, fargs=(field,),
frames=k, nterval=500, repeat=False, blit=False)https://stackoverflow.com/questions/61790019
复制相似问题