2017-01-13 4 views
1

私は多くのタイムステップで何かをシミュレートしていますが、多くのステップが必要です。私は結果をアニメーション化し、nごとに1つのステップしかプロットしません。matplotlibはアニメーションのnステップごとに保存します

今私は2つの非機能的な命題を持っています。最初のものは正しくループしていないように見えますが、2番目の方法では、図の更新されていない手順でビデオが埋められ、ファイルが大きくなり、遅くなります。

あなたは私を助けることができますか?

おかげ

X, Y = np.meshgrid(256,256) 
fig,ax = plt.subplots() 
plot_every = 50 
saved_steps = [] 
def make_step(s, t): 
c = ... 
    if (s%plot_every) == 0: 
     print("plotting step {:.0f} , t ={:.0f} ".format(s, t*dt)) 
     ax.clear() 
     ax.contourf(X, Y, c, 
       np.arange(0, 1.0, 0.01), 
       extend='both') 
     saved_steps.append(ax) 

for s , t in enumerate(range(t_steps)): 
    make_step(s,t) 

print("will now save anim") 
def plot_ani(i): 
    return saved_steps[i] 

anim = animation.FuncAnimation(fig, plot_ani,range(len(saved_steps)) , interval=500, blit=False) 

か:

fig,ax = plt.subplots() 
saved_steps = [] 
def make_step(s, t): 
    if (s%plot_every) == 0: 
     print("plotting step {:.0f} , t ={:.0f} ".format(s, t*dt)) 
     ax.clear() 
     ax.contourf(X, Y, c, 
       np.arange(0, 1.0, 0.01), 
       extend='both') 
     return ax 

anim = animation.FuncAnimation(fig, make_step,range(len(saved_steps)) , interval=500, blit=False) 

、その後、私はあなたの最初のアプローチの問題は、あなたがリストにmatplotlibのaxesオブジェクトを格納しようとしていることである

anim.save('spinodal_decompo_ex.ogv', codec='libtheora') 

答えて

0

可視化を開始し、ここで私が思い付いたものです。追加の事は、

fig = plt.figure() 

saved_steps = [] 
def make_step(i, t): 
    c = ... 
    if (i%plot_every) == 0: 
     print("plotting step {:.0f} , t ={:.0f} ".format(i, t*dt)) 
     im = plt.imshow(c, animated=True) 
     saved_steps.append([im]) 

for s , t in enumerate(range(t_steps)): 
    make_step(s, t) 

print("will now save anim") 
anim = animation.ArtistAnimation(fig, saved_steps, interval=50, blit=False) 

anim.save('spinodal_decompo_ex.ogv', codec='libtheora', extra_args=['-qscale:v', '7']) 

は、この点を指摘いただきありがとうございます... contourfは明らかに、 artistではないということです。

+0

matplotlibオブジェクトを保存すると、多くの不必要なオーバーヘッドが発生します。分離原則に従うことでこれを排除することができます。しかし、これはもちろんあなた自身の選択です。 – ImportanceOfBeingErnest

2

を行います。ただし、リストに保存するaxは常に同じです。 ax.clear()に電話すると、以前に保存されたaxオブジェクトもクリアされます。

第2のアプローチの問題は、FuncAnimationが常に各タイムステップの図を保存することです。あなたが軸の中で何かを変えるかどうかは関係ありません。

解決に向けて回す:に固執することは常に賢明である原理は、データの視覚化とは別のデータの生成や計算を維持にある

したがって、アドバイスは

です。最初にデータを計算します。

time_steps = 10000 
data = [] 
for t in range(time_steps): 
    d = calculate_data(t) 
    data.append(d) 
    # alternatively save data to file 
    # if it's to big to be kept in memory. 

だけにしてImportanceofBeingErnest回答に基づいて

# now start visualizing 
plot_every = 50 
fig, ax = plt.subplots() 

def make_step(step): 
    ax.clear() 
    # recall data from list, but take only every 50th entry 
    c = data[step*plot_every] #alternatively reload data from file 
    ax.contourf(X, Y, c, ...) 

anim = animation.FuncAnimation(fig, make_step,range(time_steps//plot_every), interval=500) 
+0

ありがとう、それは私が解決策を見つけるのを助けた! – Napseis

関連する問題