2017-11-22 5 views
1

私は、アニメーションが実行されているか停止状態になっているときにアニメーションプロットのxlimとylimを変更したいと思っています。アニメーションの実行中にそれらを変更すると(ax.set_xlim/set_ylim呼び出しを使用)、event_source.stop()を使用してアニメーションを一時停止すると、座標軸に沿って描かれた数字は変更されません。matplotlibで一時停止したアニメーションの座標範囲を調整する方法は?

ここにこの問題を示すテストプログラムがあります。アニメーションの実行中に ' - 'と '+'キーを押すと、青色のオブジェクトがスケールされ、座標範囲が更新されます。しかし、最初にSPACEを押してアニメーションを一時停止し、 ' - 'または '+'を押すと、青色のオブジェクトだけが拡大/縮小されますが、座標範囲はそのまま残ります(アニメーションは呼び出しの副作用として再開されます)。 ani._end_redraw(なし))。

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
from numpy import sin, cos, pi 

def keypress(event): 
    global anim_running 
    if event.key == ' ': 
     ani.event_source.stop() if anim_running else ani.event_source.start() 
     anim_running = not anim_running 
    elif event.key == '+': 
     ax.set_xlim([-4,4]) 
     ax.set_ylim([-4,4]) 
     ani._end_redraw(None) 
    elif event.key == '-': 
     ax.set_xlim([-1,1]) 
     ax.set_ylim([-1,1]) 
     ani._handle_resize() 
     ani._end_redraw(None) 

phi = pi/2 

def animate(i): 
    global phi 
    line.set_data([[0.0, sin(phi)], [0.0, cos(phi)]]) 
    phi += 0.01 
    return line, 

fig,ax = plt.subplots(1, 1) 
ax.set_xlim([-2,2]) 
ax.set_ylim([-2,2]) 
line, = ax.plot([], [], 'o-', lw=2, color='b') 
fig.canvas.mpl_connect('key_press_event', keypress) 
ani = animation.FuncAnimation(fig, animate, blit=True, interval=0, frames=2000) 
anim_running = True 
plt.show() 
+0

を繰り返しますが、これがあるだけで、1変更の制限、2ドローキャンバス、3.stopアニメーションになる(_end_redraw経由)、(_handle_resize経由)4.再起動のアニメーションを新しい背景をつかむだろうと思いますブリッティングの問題が使用されています。その要件は問題の一部になるはずです。 – ImportanceOfBeingErnest

答えて

1

完全なキャンバスを再描画して、観察可能な限界の変更を確実にする必要があります。私は良い戦略が

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
from numpy import sin, cos, pi 

def keypress(event): 
    global anim_running 
    if event.key == ' ': 
     ani.event_source.stop() if anim_running else ani.event_source.start() 
     anim_running = not anim_running 
    elif event.key == '+': 
     ax.set_xlim([-4,4]) 
     ax.set_ylim([-4,4]) 
     fig.canvas.draw() 
     ani._handle_resize() 
     ani._end_redraw(None) 
    elif event.key == '-': 
     ax.set_xlim([-1,1]) 
     ax.set_ylim([-1,1]) 
     fig.canvas.draw() 
     ani._handle_resize() 
     ani._end_redraw(None) 

phi = pi/2 

def animate(i): 
    global phi 
    line.set_data([[0.0, sin(phi)], [0.0, cos(phi)]]) 
    phi += 0.01 
    return line, 

fig,ax = plt.subplots(1, 1) 
ax.set_xlim([-2,2]) 
ax.set_ylim([-2,2]) 
line, = ax.plot([], [], 'o-', lw=2, color='b') 
fig.canvas.mpl_connect('key_press_event', keypress) 
ani = animation.FuncAnimation(fig, animate, blit=True, interval=2, frames=2000) 
anim_running = True 
plt.show() 
+0

ありがとうございます、はい、それは動作します!再びあなたは私を助けました。私はいつか私があなたの優しさのためにあなたに返済できるようになることを願っています! :) –

関連する問題