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

2024-04-18 01:50:15 发布

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

我使用这个函数:

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

把它叫做:

^{pr2}$

我想问一下是否可以每1秒拍一次情节(动画)的快照。在

(我使用matplotlib)


Tags: 函数infordataplotdefrangeplt
1条回答
网友
1楼 · 发布于 2024-04-18 01:50:15

您可以子类ArtistAnimation并重写步骤方法,例如:

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)

添加:

^{pr2}$

正在更改:

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

在您的示例源代码中。在

为了更好地理解做什么和如何做,我建议您看看matplotlib sources。在

相关问题 更多 >