如何要等到matplotlib动画结束? [英] How to wait until matplotlib animation ends?

查看:445
本文介绍了如何要等到matplotlib动画结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下code直接从Matplotlib文件执行:

Consider the following code directly taken from the Matplotlib documentation:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time # optional for testing only
import cv2 # optional for testing only

fig = plt.figure()   

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)

im = plt.imshow(f(x, y), animated=True)    

def updatefig(*args):
    global x, y
    x += np.pi / 15.
    y += np.pi / 20.
    im.set_array(f(x, y))
    return im,

ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True)
plt.show()

我的系统上这个做工精细。现在,尝试下面的一段code的追加上述code:

This work fine on my system. Now, try to append the following piece of code to the above code:

while True: 
  #I have tried any of these 3 commands, without success:  
    pass
    #time.sleep(1)
    #cv2.waitKey(10)

什么情况是,该程序冻结。显然,动画级Matplotlib的运行在独立的线程动画。所以,我有2个以下几个问题:

What happens is that the program freezes. Apparently, the "Animation" class of Matplotlib runs the animation in a separate thread. So I have the 2 following questions:

1)如果过程在单独的线程中运行,它为什么通过随后环路干扰

1) If the process runs in a separate thread, why is it disturbed by the subsequent loop ?

2)如何说蟒蛇等到动画已经结束?

2) How to say to python to wait until the animation has ended ?

推荐答案

感谢埃德·史密斯和MiteshNinja的帮助下,我终于成功地找到了工作不仅与IPython的控制台可靠的方法,而且还使用Python控制台和命令行。此外,它允许对动画处理的完全控制。 code是自我解释。

Thanks to the help of Ed Smith and MiteshNinja, I have finally succeeded in finding a robust method that works not only with the Ipython console, but also with the Python console and the command line. Furthermore, it allows total control on the animation process. Code is self explanatory.

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from multiprocessing import Process
import time # optional for testing only
import matplotlib.animation as animation

# A. First we define some useful tools:

def wait_fig(): 
    # Block the execution of the code until the figure is closed.
    # This works even with multiprocessing.
    if matplotlib.pyplot.isinteractive():
        matplotlib.pyplot.ioff() # this is necessary in mutliprocessing
        matplotlib.pyplot.show(block=True)
        matplotlib.pyplot.ion() # restitute the interractive state
    else:
        matplotlib.pyplot.show(block=True) 

    return    


def wait_anim(anim_flag, refresh_rate = 0.1):    
    #This will be used in synergy with the animation class in the example
    #below, whenever the user want the figure to close automatically just 
    #after the animation has ended.
    #Note: this function uses the controversial event_loop of Matplotlib, but 
    #I see no other way to obtain the desired result.

    while anim_flag[0]: #next code extracted from plt.pause(...)
        backend = plt.rcParams['backend']
        if backend in plt._interactive_bk:
            figManager = plt._pylab_helpers.Gcf.get_active()
            if figManager is not None:
                figManager.canvas.start_event_loop(refresh_rate)  


def draw_fig(fig = None):    
    #Draw the artists of a figure immediately.
    #Note: if you are using this function inside a loop, it should be less time 
    #consuming to set the interactive mode "on" using matplotlib.pyplot.ion()
    #before the loop, event if restituting the previous state after the loop.

    if matplotlib.pyplot.isinteractive():
        if fig is None:
            matplotlib.pyplot.draw()
        else: 
            fig.canvas.draw()            
    else:   
        matplotlib.pyplot.ion() 
        if fig is None:
            matplotlib.pyplot.draw()
        else: 
            fig.canvas.draw() 
        matplotlib.pyplot.ioff() # restitute the interactive state

    matplotlib.pyplot.show(block=False)
    return


def pause_anim(t): #This is taken from plt.pause(...), but without unnecessary 
                   #stuff. Note that the time module should be previously imported.
                   #Again, this use the controversial event_loop of Matplotlib. 
    backend = matplotlib.pyplot.rcParams['backend']
    if backend in matplotlib.pyplot._interactive_bk:
        figManager = matplotlib.pyplot._pylab_helpers.Gcf.get_active()
        if figManager is not None:
            figManager.canvas.start_event_loop(t)
            return
    else: time.sleep(t) 


#--------------------------

# B. Now come the particular functions that will do the job.
def f(x, y):
    return np.sin(x) + np.cos(y)


def plot_graph():
    fig = plt.figure()
    x = np.linspace(0, 2 * np.pi, 120)
    y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
    im = fig.gca().imshow(f(x, y))    
    draw_fig(fig)
    n_frames = 50

    #==============================================    
    #First method - direct animation: This use the start_event_loop, so is 
    #somewhat controversial according to the Matplotlib doc.
    #Uncomment and put the "Second method" below into comments to test.

    '''for i in range(n_frames): # n_frames iterations    
        x += np.pi / 15.
        y += np.pi / 20.
        im.set_array(f(x, y))
        draw_fig(fig)  
        pause_anim(0.015) # plt.pause(0.015) can also be used, but is slower

    wait_fig() # simply suppress this command if you want the figure to close 
               # automatically just after the animation has ended     
    '''    
    #================================================
    #Second method: this uses the Matplotlib prefered animation class.    
    #Put the "first method" above in comments to test it.
    def updatefig(i, fig, im, x, y, anim_flag, n_frames):
        x = x + i * np.pi / 15.
        y = y + i * np.pi / 20.
        im.set_array(f(x, y))        

        if i == n_frames-1:
            anim_flag[0] = False

    anim_flag = [True]    
    animation.FuncAnimation(fig, updatefig, repeat = False, frames = n_frames, 
         interval=50, fargs = (fig, im, x, y, anim_flag, n_frames), blit=False) 
                            #Unfortunately, blit=True seems to causes problems

    wait_fig()  
    #wait_anim(anim_flag) #replace the previous command by this one if you want the 
                     #figure to close automatically just after the animation 
                     #has ended                                                                
    #================================================           
    return

#--------------------------

# C. Using multiprocessing to obtain the desired effects. I believe this 
# method also works with the "threading" module, but I haven't test that.

def main() # it is important that ALL the code be typed inside 
           # this function, otherwise the program will do weird 
           # things with the Ipython or even the Python console. 
           # Outside of this condition, type nothing but import
           # clauses and function/class definitions.
    if __name__ != '__main__': return                      
    p = Process(target=plot_graph)
    p.start()
    print('hello', flush = True) #just to have something printed here
    p.join() # suppress this command if you want the animation be executed in
             # parallel with the subsequent code
    for i in range(3): # This allows to see if execution takes place after the 
                       #process above, as should be the case because of p.join().
        print('world', flush = True) 
        time.sleep(1)        

main()

这篇关于如何要等到matplotlib动画结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆