![](/img/trans.png)
[英]Pause/Stop animation of matplotlib on button press embedded in tkinter
[英]stop / start / pause in python matplotlib animation
我在 matplotlib 的動畫模塊中使用 FuncAnimation 來制作一些基本動畫。 這個函數永遠循環播放動畫。 有沒有一種方法可以讓我通過鼠標點擊來暫停和重新啟動動畫?
"這是一個 FuncAnimation 示例,我將其修改為在鼠標點擊時暫停。 由於動畫是由生成器函數simData
驅動的,當全局變量pause
為 True 時,產生相同的數據會使動畫看起來暫停。
paused
的值通過設置事件回調來切換:
def onClick(event):
global pause
pause ^= True
fig.canvas.mpl_connect('button_press_event', onClick)
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
pause = False
def simData():
t_max = 10.0
dt = 0.05
x = 0.0
t = 0.0
while t < t_max:
if not pause:
x = np.sin(np.pi*t)
t = t + dt
yield x, t
def onClick(event):
global pause
pause ^= True
def simPoints(simData):
x, t = simData[0], simData[1]
time_text.set_text(time_template%(t))
line.set_data(t, x)
return line, time_text
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot([], [], 'bo', ms=10)
ax.set_ylim(-1, 1)
ax.set_xlim(0, 10)
time_template = 'Time = %.1f s'
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
fig.canvas.mpl_connect('button_press_event', onClick)
ani = animation.FuncAnimation(fig, simPoints, simData, blit=False, interval=10,
repeat=True)
fig.show()
這工作...
anim = animation.FuncAnimation(fig, animfunc[,..other args])
#pause
anim.event_source.stop()
#unpause
anim.event_source.start()
在這里結合@fred 和@unutbu 的回答,我們可以在創建動畫后添加一個 onClick 函數:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
def run_animation():
anim_running = True
def onClick(event):
nonlocal anim_running
if anim_running:
anim.event_source.stop()
anim_running = False
else:
anim.event_source.start()
anim_running = True
def animFunc( ...args... ):
# Animation update function here
fig.canvas.mpl_connect('button_press_event', onClick)
anim = animation.FuncAnimation(fig, animFunc[,...other args])
run_animation()
現在我們可以簡單地通過點擊停止或開始動畫。
我登陸這個頁面試圖實現相同的功能,暫停 matplotlibs 動畫。 其他答案很好,但除此之外,我還希望能夠使用箭頭鍵手動循環瀏覽幀。 對於任何尋找相同功能的人,這是我的實現:
import matplotlib.pyplot as plt
import matplotlib.animation as ani
fig, ax = plt.subplots()
txt = fig.text(0.5,0.5,'0')
def update_time():
t = 0
t_max = 10
while t<t_max:
t += anim.direction
yield t
def update_plot(t):
txt.set_text('%s'%t)
return txt
def on_press(event):
if event.key.isspace():
if anim.running:
anim.event_source.stop()
else:
anim.event_source.start()
anim.running ^= True
elif event.key == 'left':
anim.direction = -1
elif event.key == 'right':
anim.direction = +1
# Manually update the plot
if event.key in ['left','right']:
t = anim.frame_seq.next()
update_plot(t)
plt.draw()
fig.canvas.mpl_connect('key_press_event', on_press)
anim = ani.FuncAnimation(fig, update_plot, frames=update_time,
interval=1000, repeat=True)
anim.running = True
anim.direction = +1
plt.show()
一些注意事項:
running
和direction
的值,我將它們分配給anim
。 它避免使用非本地(Python2.7 中不可用)或全局(不可取,因為我在另一個函數中運行此代碼)。 不確定這是否是好的做法,但我發現它非常優雅。anim
的生成器對象。 這確保當我恢復動畫時,它從活動幀開始,而不是從最初暫停的位置開始。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.