【问题标题】:take snapshots every second using artist animation使用艺术家动画每秒拍摄快照
【发布时间】:2012-03-01 23:50:28
【问题描述】:

我使用这个功能:

def Plot(data):

    plt.colormaps()
    n=sc.shape(data)[2]
    ims=[]
    for i in range(n):
        mydata=data[:,:,i]
        im=plt.imshow(mydata,cmap=plt.get_cmap('jet'))
        ims.append([im])
    return ims

然后这样称呼它:

fig=plt.gcf()
ani=ArtistAnimation(fig,result,interval=10,repeat=False)
plt.show()

例如,我想问是否可以每 1 秒拍摄一次情节(动画)的快照。

(我使用matplotlib)

【问题讨论】:

  • 我猜你没有得到答案,因为你没有为你的问题提供足够的上下文信息。对于初学者:您使用的是哪个框架? Matplotlib?
  • @Don 问题:是的,matplotlib。我有它作为标签。

标签: python matplotlib


【解决方案1】:

您可以继承 ArtistAnimation 并覆盖 _step - 方法,例如:

class SnapShotAnimation(ArtistAnimation):
    def __init__(self, fig, artists, snapshot_delay, *args, **kwargs):
        self._snapshot_delay = snapshot_delay
        self._time_to_snapshot = snapshot_delay
        ArtistAnimation.__init__(self, fig, artists, *args, **kwargs)

    def _step(self, *args):
        if self._time_to_snapshot <= 0:
            do_snapshot() 
            self._time_to_snapshot = self._snap_shot_delay #reset timer
        else:
            self._time_to_snapshot -= self._interval
        ArtistAnimation._step(*args) #ancestor method maybe better at start

    def do_snapshot(self):
        """Your actual snapshot code comes here - basically saving to a output"""
        fname = 'snapshot.png'
        self._fig.savefig(fname)

添加:

snapshot_delay = 1000 # time in ms

改变:

ani=SnapShotAnimation(fig,result,snapshot_delay, interval=10,repeat=False)

在您的示例源中。

为了更好地了解做什么和如何做,我建议查看matplotlib sources

【讨论】:

  • :你好,我不明白在“do_snapshot”中放什么。如果我运行它,它会给我“__init__() 至少需要 3 个参数(2 个给定)”。另外,在 init (...snapshot_delay,*args..) 的参数中加上逗号。谢谢!
  • :它继续给我“__init__() 至少需要 3 个参数(给定 2 个)”。
  • 是的,继承的构造函数调用不完整。我不能也不会提供完整的解决方案,只是想帮助您入门。请记住,我的 do_snapshot 方法是一个示例,您必须对其进行修改以满足您的需求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-14
  • 2014-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-10
相关资源
最近更新 更多