Make interactive matplotlib window not pop to front on each update (Windows 7)

前端 未结 4 1294
北恋
北恋 2020-12-11 01:28

Today I upgraded matplotlib to version 2.0.2, after not upgrading for possibly 3 years.

Now I have the problem that in interactive plots the window always comes to t

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-11 01:38

    Changing the backend

    The issue seems only present using the Tk backend. Using the Qt backend, the window would stay where it was while updating with plt.pause.

    To change the backend use those lines at the beginning of your script.

    import matplotlib
    matplotlib.use("Qt4agg") # or "Qt5agg" depending on you version of Qt
    

    Modifying plt.pause

    If changing the backend is not an option, the following might help. The cause of the window constantly popping up to the front comes from plt.pause calling plt.show() internally. You therefore implement you own pause function, without calling show. This requires to be in interactive mode plt.ion() first and then at least once call plt.show(). Afterwards you may update the plot with the custom mypause function as shown below.

    import matplotlib
    matplotlib.use("TkAgg")
    import matplotlib.pyplot as plt
    from time import time
    from random import random
    
    plt.ion()
    # set up the figure
    fig = plt.figure()
    plt.xlabel('Time')
    plt.ylabel('Value')
    
    plt.show(block=False)
    
    def mypause(interval):
        backend = plt.rcParams['backend']
        if backend in matplotlib.rcsetup.interactive_bk:
            figManager = matplotlib._pylab_helpers.Gcf.get_active()
            if figManager is not None:
                canvas = figManager.canvas
                if canvas.figure.stale:
                    canvas.draw()
                canvas.start_event_loop(interval)
                return
    
    
    t0 = time()
    t = []
    y = []
    while True:
        t.append( time()-t0 )
        y.append( random() )
        plt.gca().clear()
        plt.plot( t , y )
        mypause(1)
    

    Using an animation.

    Finally, using a matplotlib.animation class would render all of the above obsolete. An example for matplotlib.animation.FuncAnimation is shown on the matplotlib page.

提交回复
热议问题