且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-08-21 11:59:58

您可以子类化 ArtistAnimation 并覆盖 _step - 方法,例如:

You could subclass ArtistAnimation and overwrite the _step - method, e.g.:

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 来源.

For better understanding what and how to do, i would recommend to take a look into the matplotlib sources.