2017-03-01 9 views
0

imshow animate using ArtistAnimationの例を使用して、ファイルから取得した一連の2D配列をアニメーション化しようとしています。これを行うには、関数内でArtistAnimationを使用する必要がありますが、この単純な変更は私にはわからないTypeErrorを与えます。私が行った変更は、次のとおりです。Matplotlibを使用した例外タイプエラーimshow animate with ArtistAnimation

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




def f(x, y): 
    return np.sin(x) + np.cos(y) 

x = np.linspace(0, 2 * np.pi, 120) 
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1) 
# ims is a list of lists, each row is a list of artists to draw in the 
# current frame; here we are just animating one artist, the image, in 
# each frame 
def animate(x,y): 
    fig = plt.figure() 
    ims = [] 
    for i in range(60): 
     x += np.pi/15. 
     y += np.pi/20. 
     im = plt.imshow(f(x, y), animated=True) 
     ims.append([im]) 


    ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True, 
           repeat_delay=1000) 

    # ani.save('dynamic_images.mp4') 

    plt.show() 

そして、私は受信TypeError例外メッセージです:

In [23]: animate(x,y) 
Exception TypeError: TypeError("'instancemethod' object is not connected",) in <bound method TimerQT.__del__ of <matplotlib.backends.backend_qt5.TimerQT object at 0x7f964ea06150>> ignored 
+0

ここに示すコードはほとんど意味がありません。この例を変更する理由は何ですか? – ImportanceOfBeingErnest

答えて

2

animation documentationが言うように:「インスタンスオブジェクトへの参照を保持することが重要です」。
また、最後にplt.show()に電話する必要があります。
これはあなたが

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


def f(x, y): 
    return np.sin(x) + np.cos(y) 

x = np.linspace(0, 2 * np.pi, 120) 
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1) 

def animate(x,y): 
    fig = plt.figure() 
    ims = [] 
    for i in range(60): 
     x += np.pi/15. 
     y += np.pi/20. 
     im = plt.imshow(f(x, y), animated=True) 
     ims.append([im]) 

    ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True, 
           repeat_delay=1000) 

    return ani 

のようなものにコードを変更し、その後に

ani = animate(x,y) 
plt.show() 

ようにそれを呼び出すために必要があると思いjupyterノートブックで、スクリプトとして実行されている問題ではありませんがアニメーションへの参照を保持する。

関連する問題