Matplotlib.animation.FuncAnimation一次迭代一次列表

2024-10-03 19:32:52 发布

您现在位置:Python中文网/ 问答频道 /正文

目前我正在处理一个要分析的曲面,4000个40x40帧。机械上它是一个4000帧的列表,它是一个40行的列表,它是一个40个数据点的列表。在

我使用funcanimation来查看它是如何随时间变化的,但是我遇到了一个问题。在这一点上,我有两个问题:

1)如何迭代帧一次,最终用于保存?在

2)如果我试图退出动画,为什么我当前的代码会给我一个旋转的海滩球?在

def update_fig(*args):
  global currentNum, numSurfaces
  currentNum += 1
  if currentNum < numSurfaces:
    print(currentNum)
  im.set_array(testClass.surfaceList[currentNum])
  return im,
    else:
      pass


def updatefig(*args):
  global currentNum, numSurfaces
  currentNum += 1
  if currentNum > numSurfaces - 1:
    currentNum = 0
  print currentNum
  im.set_array(testClass.surfaceList[currentNum])
  return im,

ani = animation.FuncAnimation(fig, updatefig, frames=4000, interval=0.5, repeat=False, blit=False)
plt.show()
plt.close()

Tags: 列表returnifdeffigargsarrayglobal
1条回答
网友
1楼 · 发布于 2024-10-03 19:32:52

在开始动画之前,可以通过在帧上循环来保存图形。在

def updatefig(i):
    im.set_array(testClass.surfaceList[i])

for i in range(4000):
    updatefig(i)
    plt.savefig("frame{}.png".format(i))

ani = animation.FuncAnimation(fig, updatefig, frames=4000, interval=0.5, repeat=False)
plt.show()

也可以使用动画保存图形

^{pr2}$

第二种方法只适用于png图像,并且需要安装imagemagick。在

相关问题 更多 >