本文介绍了使用艺术家动画每秒拍摄快照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用此功能:
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 秒拍摄一次情节(动画)的快照.
I want to ask if it possible to take snapshots of the plot(animation) for every 1 second for example.
(我使用matplotlib)
(I use matplotlib)
推荐答案
您可以子类化 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.
这篇关于使用艺术家动画每秒拍摄快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!