2017-09-05 5 views
2

私は、いくつかのグラフを生成した後、フォートランでいくつかのシミュレーションから出力データを読み、軌道の動画を作ります。最初は、アニメーションにblittingを使用しなかったので、動作していた間は非常に遅かったです。Matplotlibのアニメーション更新機能がデータを設定していません

元々私が欲しがっていたアニメーションは、後に続くエフェクトを作成するために、アルファが減少する5つの一連のデータがあるため、自分自身に分散させたいと思っていました。私は、スクリプトを実行すると、ウィンドウが一瞬のために表示され、画面上の黄色の円が示す、非常に明確にあり、今

def animate(frame): 
    jptx, jpty = jx[frame-3:frame], jy[frame-3:frame] 
    cptx, cpty = cx[frame-3:frame], cy[frame-3:frame] 
    eptx, epty = ex[frame-3:frame], ey[frame-3:frame] 
    gptx, gpty = gx[frame-3:frame], gy[frame-3:frame] 
    iptx, ipty = ix[frame-3:frame], iy[frame-3:frame] 
    ax2.clear() 
    ax2.scatter(jptx, jpty, s=32, c=ablue, marker="s", label='Jupiter') 
    ax2.scatter(cptx, cpty, s=8, c=ared, marker="o", label='Callisto') 
    ax2.scatter(eptx, epty, s=8, c=agreen, marker="o", label='Europa') 
    ax2.scatter(gptx, gpty, s=8, c=ablack, marker="o", label='Ganymede') 
    ax2.scatter(iptx, ipty, s=8, c=ayellow, marker="o", label='Io') 
    ax2.set_xlim(-3, 7) 
    ax2.set_ylim(-3, 4) 
animation = animation.FuncAnimation(fig2, animate, interval=0.5, frames=jt.size) 
print('Begin saving animation') 
animation.save('Tabbys Star.mp4', writer='ffmpeg', fps=60) 
print('Animation saved') 
plt.show() 

:ここに私の元の(非ブリット)更新機能です背景が描画されています。ただし、ウィンドウはすぐに終了します。これは、2回目の試みに関連するコードです。この試行で黄色の円が追加されました。

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

# j_file = location + 'JUPITER.aei' 
# jt, jx, jy, jz = read_data(j_file) 
jt, jx, jy, jz = np.random.random([100,4]), np.random.random([100,4]), np.random.random([100,4]), np.random.random([100,4]) 

# c_file = location + 'CALLISTO.aei' 
# ct, cx, cy, cz = read_data(c_file) 
ct, cx, cy, cz = np.random.random([100,4]), np.random.random([100,4]), np.random.random([100,4]), np.random.random([100,4]) 

alphas = [0.25, 0.5, 0.75, 1] 

ablue = np.zeros((4, 4)) 
ablue[:, 2] = 1.0 
ablue[:, 3] = alphas 

ared = np.zeros((4, 4)) 
ared[:, 0] = 1.0 
ared[:, 3] = alphas 

fig2 = plt.figure() 
ax2 = fig2.add_subplot(111, aspect='equal') 
xdata, ydata = np.zeros((4,)), np.zeros((4,)) 
jpt, = plt.plot(xdata, ydata, marker='.', ms=32, c=ablue, label='Jupiter') 
cpt, = plt.plot(xdata, ydata, marker='.', ms=8, c=ared, label='Callisto') 


def init(): 
    ax2.set_xlim(-3, 7) 
    ax2.set_ylim(-3, 4) 
    circle = plt.Circle((0, 0), 0.1, color='y') 
    ax2.add_patch(circle) 
    for pt in [jpt, cpt]: 
     pt.set_data(np.zeros((4,)), np.zeros((4,))) 
    return jpt, cpt 


def animate(frame, j, c): 
    jptx, jpty = jx[frame-3:frame], jy[frame-3:frame] 
    cptx, cpty = cx[frame-3:frame], cy[frame-3:frame] 
    j.set_data(jptx, jpty) 
    c.set_data(cptx, cpty) 
    return j, c 


animation = animation.FuncAnimation(fig2, animate, fargs=(jpt, cpt), interval=0.5, frames=jt.size, init_func=init, blit=True) 
print('Begin saving animation') 
# animation.save('Tabbys Star.mp4', writer='ffmpeg', fps=60) 
print('Animation saved') 
plt.show() 

私はまた、最終的に伝説といくつかの軸ラベルを追加したいが、私はそれが正常に行うことができると信じています。

2番目のコードスニペットでアニメーションの問題は何ですか? (再び)わかりやすくするために編集された

おかげ

+0

ようになり、コードが実行可能であるように。そのような[mcve]を含めるように質問を編集したら、人々が手助けすることができます。必要な情報には、スクリプトの実行方法(環境、バージョンなど)も含まれます。 – ImportanceOfBeingErnest

+0

明瞭にするために編集されました。天文学データの代わりに、データ(例えば、上記のjt、jx、jy、jz)は、同じ長さのnp 1次元配列(すなわち、jt.size = ix.sizeなど)としてシミュレートすることができます –

+0

[mcve]誰かが質問からコードをコピーしてコピーして実行し、問題の問題を観察できるということです。代わりに、質問からの例から20分の例を一緒に微調整する必要がある場合、私はすぐに手を差し伸べることなくあきらめるだろう。 – ImportanceOfBeingErnest

答えて

0

あなたは高い値にsettingframesによって、より1フレームのためにそれをレンダリングすることを、確認してください。投稿したコードでは、フレーム数が明確に定義されていないため、この問題が発生する可能性があります。

+0

私の2回目の試みでは、収集するデータポイントの数であるframes = jt.sizeを設定します。 –

+0

しかし、それはとにかくkwargを指定せずに最初の試行でまだ働いていました –

+0

確かに、 'jt'が定義されていますか?投稿されたコードでは定義されていないためです。 – FlashTek

0

ここではplt.plotplt.scatterを混同しています。あなたが得るエラーは、アニメーションなしで生成されることさえあります。

plt.plotには、それぞれ色とマーカーサイズを設定する引数がcolormsですが、異なる点で異なる値を使用することはできません。このため、プロットはscatterです。 plt.scatterには、それぞれ色とマーカーサイズを設定する引数csがあります。

異なる色の点を取得するには、散布図を使用する必要があります。

jpt = plt.scatter(xdata, ydata, marker='.', s=32, c=ablue, label='Jupiter') 

は、その後、アニメーションのためにあなたはそれが.set_dataメソッドを持っていないので、scatterで使用するためにあなたのコードを調整する必要がありますが、2列配列の入力を期待.set_offsets方法でしょう。

j.set_offsets(np.c_[jptx, jpty]) 

合計では、スクリプトがあなたが定義されているすべての関連する変数と取り残さすべての無関係なもので、[MCVE]問題のを提供する必要が

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


jt, jx, jy, jz = [np.random.random([100,4]) for _ in range(4)] 
ct, cx, cy, cz = [np.random.random([100,4]) for _ in range(4)] 

alphas = [0.25, 0.5, 0.75, 1] 

ablue = np.zeros((4, 4)) 
ablue[:, 2] = 1.0 
ablue[:, 3] = alphas 

ared = np.zeros((4, 4)) 
ared[:, 0] = 1.0 
ared[:, 3] = alphas 

fig2 = plt.figure() 
ax2 = fig2.add_subplot(111, aspect='equal') 
xdata, ydata = np.zeros((4,)), np.zeros((4,)) 

jpt = plt.scatter(xdata, ydata, marker='.', s=32, c=ablue, label='Jupiter') 
cpt = plt.scatter(xdata, ydata, marker='.', s=8, c=ared, label='Callisto') 


def init(): 
    ax2.axis([0,1,0,1]) 
    circle = plt.Circle((0, 0), 0.1, color='y') 
    ax2.add_patch(circle) 
    for pt in [jpt, cpt]: 
     pt.set_offsets(np.c_[np.zeros((4,)), np.zeros((4,))]) 
    return jpt, cpt 


def animate(frame, j, c): 
    jptx, jpty = jx[frame-3:frame], jy[frame-3:frame] 
    cptx, cpty = cx[frame-3:frame], cy[frame-3:frame] 
    j.set_offsets(np.c_[jptx, jpty]) 
    c.set_offsets(np.c_[cptx, cpty]) 
    return j, c 


animation = animation.FuncAnimation(fig2, animate, fargs=(jpt, cpt), 
            interval=50, frames=jt.size, init_func=init, blit=True) 

plt.show() 
+0

ありがとう!ご面倒をおかけしてすみません。あなたはおそらく私がここに新しいと言うことができます –

+0

私はフォローアップを尋ねることができます、なぜそれは背景がまだ描かれているのですか?この監視から明示的なエラーメッセージが表示されないのはなぜですか? –

+0

明示的なエラーが発生するはずです(ただし、コードの実行方法がわからないため、わかりません)。描画時間にエラーが発生するので(レンダラーが色分けできない色を見つけた時点で)、残りのプロットは多かれ少なかれ正しく描画されます。 – ImportanceOfBeingErnest

関連する問題