使用艺术家动画每秒拍摄快照

时间:2012-03-01 10:19:57

标签: python matplotlib

我使用这个功能:

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)

1 个答案:

答案 0 :(得分:2)

您可以继承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