2015-11-19 118 views
0

我正在繪製圓圈動畫。只要speed設置爲正數,它看起來很好,並且效果很好。但是,我想將speed設置爲0.0。當我這樣做時,一些東西會改變,它不再具有動畫效果。相反,我必須在每幀之後單擊窗口上的「x」。我嘗試使用plt.draw()plt.show()的組合以獲得與plt.pause()相同的效果,但不顯示框架。如何正確地複製plt.pause()的功能,而無需涉及定時器,或者將其設置爲0.0如何pyplot暫停()零時間?

speed = 0.0001 
plt.ion() 
for i in range(timesteps): 
    fig, ax = plt.subplots() 
    for j in range(num): 
     circle = plt.Circle(a[j], b[j]), r[j], color='b') 
     fig.gca().add_artist(circle) 
    plt.pause(speed) 
    #plt.draw() 
    #plt.show() 
    plt.clf() 
    plt.close() 

回答

0

我已經複製的pyplot.pause()這裏的代碼:

def pause(interval): 
    """ 
    Pause for *interval* seconds. 

    If there is an active figure it will be updated and displayed, 
    and the GUI event loop will run during the pause. 

    If there is no active figure, or if a non-interactive backend 
    is in use, this executes time.sleep(interval). 

    This can be used for crude animation. For more complex 
    animation, see :mod:`matplotlib.animation`. 

    This function is experimental; its behavior may be changed 
    or extended in a future release. 

    """ 
    backend = rcParams['backend'] 
    if backend in _interactive_bk: 
     figManager = _pylab_helpers.Gcf.get_active() 
     if figManager is not None: 
      canvas = figManager.canvas 
      canvas.draw() 
      show(block=False) 
      canvas.start_event_loop(interval) 
      return 

    # No on-screen figure is active, so sleep() is all we need. 
    import time 
    time.sleep(interval) 

正如你可以看到,它調用start_event_loop,開始對interval秒單獨原油事件循環。如果interval == 0似乎是backend-dependend,會發生什麼情況。例如,對於WX後端,值爲0意味着此循環被阻塞並且永不結束(我必須在這裏查看the code,它不會顯示在文檔中,請參閱第773行)。

總之,0是一個特例。你不能將它設置爲一個很小的值,例如0.1秒?

以上pause文檔字符串表示,它只能用於原油anmiations,你可能有,如果你想要的東西更復雜訴諸animation module