George
George

Reputation: 5681

take snapshots every second using artist animation

I use this function:

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

and call it like:

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

I want to ask if it possible to take snapshots of the plot(animation) for every 1 second for example.

(I use matplotlib)

Upvotes: 2

Views: 787

Answers (1)

Don Question
Don Question

Reputation: 11614

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)

adding:

snapshot_delay = 1000 # time in ms

changing:

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

in your example source.

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

Upvotes: 2

Related Questions