2017-11-21 15 views
0

matplotlibのArtistAnimationを使ってみました。図のテキストとタイトルは、各フレームで変更されるはずですが、変更されません。 私は同様の問題についてたくさんの記事を読んだことがありますが、解決策が何であるかはまだ分かりません。デモではタイトルの更新は表示されません。matplotlib artist animation:タイトルまたはテキストが変更されない

誰もが知っている場合、私は感謝します!

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

fig =plt.figure()  
ims=[] 

for iternum in range(4): 
    plt.title(iternum) 
    plt.text(iternum,iternum,iternum) 
    ims.append([plt.scatter(np.random.randint(0,10,5), np.random.randint(0,20,5),marker='+' )]) 
    #plt.cla() 


ani = animation.ArtistAnimation(fig, ims, interval=500, blit=False, 
           repeat_delay=2000) 
plt.show() 

答えて

1

アーティストをアニメーション化するには、Textオブジェクトを含むあなたのims[]、アレイ内の各アーティストへの参照を返すことがあります。

ただし、タイトルはで動作しませんが、なぜわかりませんか。おそらく、関係するメカニズムをよりよく理解している人が私たちを啓発することができるでしょう。

にもかかわらず、タイトルだけでTextオブジェクトであるので、我々が使用して所望の効果を生み出すことができます。

あなたが系列のリストとしてアニメーション化するアーティストを提供する必要があり
fig = plt.figure()  
ims=[] 

for iternum in range(4): 
    ttl = plt.text(0.5, 1.01, iternum, horizontalalignment='center', verticalalignment='bottom', transform=ax.transAxes) 
    txt = plt.text(iternum,iternum,iternum) 
    ims.append([plt.scatter(np.random.randint(0,10,5), np.random.randint(0,20,5),marker='+' ), ttl, txt]) 
    #plt.cla() 


ani = animation.ArtistAnimation(fig, ims, interval=500, blit=False, 
           repeat_delay=2000) 

enter image description here

+0

ワウ!説明とコードをありがとう!私はそれをやろうと3時間以上を過ごしました。私はそれをまったくやり遂げることはできなかったと思います。私は、彼らはあなただけ – user3177938

1

ArtistAnimation。質問のコードでは、散布図だけを提供しますが、テキストとタイトルは提供しません。
タイトルは軸の一部でもあり、供給されても変更されません。通常のテキストを代わりに使用することができます。

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

fig, ax = plt.subplots() 
ims=[] 

for iternum in range(4): 
    title = plt.text(0.5,1.01,iternum, ha="center",va="bottom",color=np.random.rand(3), 
        transform=ax.transAxes, fontsize="large") 
    text = ax.text(iternum,iternum,iternum) 
    scatter = ax.scatter(np.random.randint(0,10,5), np.random.randint(0,20,5),marker='+') 
    ims.append([text,scatter,title,]) 


ani = animation.ArtistAnimation(fig, ims, interval=500, blit=False, 
           repeat_delay=2000) 
plt.show() 

あなたはFuncAnimation代わりのArtistAnimationを使用して検討することができます。これにより、タイトルを簡単に変更することができます。

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

fig, ax = plt.subplots() 
ims=[] 

text = ax.text(0,0,0) 
scatter = ax.scatter(np.random.randint(0,10,5), np.random.randint(0,20,5),marker='+') 


def update(iternum): 
    plt.title(iternum) 
    text.set_position((iternum, iternum)) 
    text.set_text(str(iternum)) 
    scatter.set_offsets(np.random.randint(0,10,(5,2))) 

ani = animation.FuncAnimation(fig, update, frames=4, interval=500, blit=False, 
           repeat_delay=2000) 
plt.show() 
+0

はあまりそれが完璧に動作ありがとう:-) matplotlibのアニメーションのチュートリアルで行ったこの1のような例をつけてほしいです!このアーティストのコンセプトを理解するためにはもう少し作業をしなければならないでしょう。 – user3177938

関連する問題