11

FuncAnimation プロット (blit を使用) でアニメーション タイトルを動作させる方法がわかりません。http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/およびPython/Matplotlib - Quickly Updating Text on Axesに基づいて、アニメーションを作成しましたが、テキスト部分が勝ちましたアニメ化しません。簡単な例:

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

vls = np.linspace(0,2*2*np.pi,100)

fig=plt.figure()
img, = plt.plot(np.sin(vls))
ax = plt.axes()
ax.set_xlim([0,2*2*np.pi])
#ttl = ax.set_title('',animated=True)
ttl = ax.text(.5, 1.005, '', transform = ax.transAxes)

def init():
    ttl.set_text('')
    img.set_data([0],[0])
    return img, ttl
def func(n):
    ttl.set_text(str(n))
    img.set_data(vls,np.sin(vls+.02*n*2*np.pi))
    return img, ttl

ani = animation.FuncAnimation(fig,func,init_func=init,frames=50,interval=30,blit=True)

plt.show()

を削除すると、テキストblit=Trueは表示されますが、速度が遅くなります。plt.titleax.set_title、および で失敗するようax.textです。

編集:最初のリンクの 2 番目の例が機能する理由がわかりました。テキストはimgパーツの中にありました。上記1.005を a.99にすると、私の言いたいことがわかるでしょう。おそらく、バウンディングボックスでこれを行う方法があるでしょう...

4

3 に答える 3

20

matplotlib の軸/目盛りのアニメーション化およびpython matplotlib blit を図の軸または側面に参照してください。

したがって、問題はanimation、ブリットの背景が実際に保存されている場所 ( animation.pyの 792 行目) の内部で、のバウンディング ボックスにあるものを取得することです。これは、複数の軸が個別にアニメーション化されている場合に意味があります。あなたの場合axes、心配する必要があるのは1つだけで、軸のバウンディングボックスの外側のものをアニメーション化したいと考えています。少しのモンキー パッチ、mpl の内臓に到達して少し突っつくことに対する許容レベル、および最も迅速で最も汚いソリューションの受け入れにより、次のように問題を解決できます。

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

def _blit_draw(self, artists, bg_cache):
    # Handles blitted drawing, which renders only the artists given instead
    # of the entire figure.
    updated_ax = []
    for a in artists:
        # If we haven't cached the background for this axes object, do
        # so now. This might not always be reliable, but it's an attempt
        # to automate the process.
        if a.axes not in bg_cache:
            # bg_cache[a.axes] = a.figure.canvas.copy_from_bbox(a.axes.bbox)
            # change here
            bg_cache[a.axes] = a.figure.canvas.copy_from_bbox(a.axes.figure.bbox)
        a.axes.draw_artist(a)
        updated_ax.append(a.axes)

    # After rendering all the needed artists, blit each axes individually.
    for ax in set(updated_ax):
        # and here
        # ax.figure.canvas.blit(ax.bbox)
        ax.figure.canvas.blit(ax.figure.bbox)

# MONKEY PATCH!!
matplotlib.animation.Animation._blit_draw = _blit_draw

vls = np.linspace(0,2*2*np.pi,100)

fig=plt.figure()
img, = plt.plot(np.sin(vls))
ax = plt.axes()
ax.set_xlim([0,2*2*np.pi])
#ttl = ax.set_title('',animated=True)
ttl = ax.text(.5, 1.05, '', transform = ax.transAxes, va='center')

def init():
    ttl.set_text('')
    img.set_data([0],[0])
    return img, ttl

def func(n):
    ttl.set_text(str(n))
    img.set_data(vls,np.sin(vls+.02*n*2*np.pi))
    return img, ttl

ani = animation.FuncAnimation(fig,func,init_func=init,frames=50,interval=30,blit=True)

plt.show()

Figure に複数の軸がある場合、これは期待どおりに機能しない可能性があることに注意してください。より良い解決策は、タイトルと軸の目盛りラベルをキャプチャするのに十分なだけaxes.bbox 拡張することです。それを行うためのコードが mpl のどこかにあるのではないかと思いますが、頭のどこにあるのかわかりません。

于 2013-07-10T05:03:03.773 に答える
1

電話する必要があります

plt.draw()

ttl.set_text(str(n))

ここに、"FuncAnimation() を使用しない" 図形内のテキスト アニメーションの非常に単純な例があります。試してみると、それがあなたにとって役立つかどうかがわかります。

import matplotlib.pyplot as plt
import numpy as np
titles = np.arange(100)
plt.ion()
fig = plt.figure()
for text in titles:
    plt.clf()
    fig.text(0.5,0.5,str(text))
    plt.draw()
于 2013-07-09T22:33:17.433 に答える