2017-06-23 31 views
1

私はmatplotlib.animationとFuncAnimationを使ってアニメーションを作った。 私はアニメーションを再生するためにTrue/Falseにリピートを設定できることを知っていますが、FuncAnimationが返ってからアニメーションを再生する方法もありますか?Python matplotlibアニメーションリピート

anim = FuncAnimation(fig, update, frames= range(0,nr_samples_for_display), blit=USE_BLITTING, interval=5,repeat=False) 

plt.show() 
playvideo = messagebox.askyesno("What to do next?", "Play Video again?") 

アニメーションオブジェクトを使用してアニメーションを再生したり、別のplt.show()を実行できますか?種類に関してあなたの答え 、数字の後 ジェラルド

答えて

1

を事前に

おかげで、それはplt.show()を用いて、第2の時間を示すことができない、一度示されています。

オプションは、Figureを再作成して新しいFigureを再度表示することです。

createfig(): 
    fig = ... 
    # plot something 
    def update(i): 
     #animate 
    anim = FuncAnimation(fig, update, ...) 

createfig() 
plt.show() 

while messagebox.askyesno(..): 
    createfig() 
    plt.show() 

おそらく、アニメーションを再起動するためのより良いオプションは、ユーザーダイアログをGUIに統合することです。これは、アニメーションの終わりに、実際にmatplotlibウィンドウを最初に閉じずに、アニメーションを再生するかどうかをユーザに尋ねることを意味します。スタートにアニメーションをリセットするには、あなたが

ani.frame_seq = ani.new_frame_seq() 

例使用したい:

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
import numpy as np 
import Tkinter 
import tkMessageBox 

y = np.cumsum(np.random.normal(size=18)) 
x = np.arange(len(y)) 

fig, ax=plt.subplots() 

line, = ax.plot([],[], color="crimson") 

def update(i): 
    line.set_data(x[:i],y[:i]) 
    ax.set_title("Frame {}".format(i)) 
    ax.relim() 
    ax.autoscale_view() 
    if i == len(x)-1: 
     restart() 

ani = animation.FuncAnimation(fig,update, frames=len(x), repeat=False) 

def restart(): 
    root = Tkinter.Tk() 
    root.withdraw() 
    result = tkMessageBox.askyesno("Restart", "Do you want to restart animation?") 
    if result: 
     ani.frame_seq = ani.new_frame_seq() 
     ani.event_source.start() 
    else: 
     plt.close() 

plt.show() 
+0

パーフェクトを、私はこのアプローチをまだ考えていなかったが、それ今の素敵なソリューション。ありがとうございました! – Grard