How to keep matplotlib (python) window in background?

后端 未结 1 1173
小鲜肉
小鲜肉 2021-01-01 17:04

I have a python / matplotlib application that frequently updates a plot with new data coming in from a measurement instrument. The plot window should not change from backgro

相关标签:
1条回答
  • 2021-01-01 17:55

    matplotlib was changed somewhere from version 1.5.2rc to 2.0.0 such that pyplot.show() brings the window to the foreground (see here). The key is therefore to avoid calling pyplot.show() in the loop. The same goes for pyplot.pause().

    Below is a working example. This will still bring the window to the foreground at the beginning. But the user may move the window to the background, and the window will stay there when the figure is updated with new data.

    Note that the matplotlib animation module might be a good choice to produce the plot shown in this example. However, I couldn't make the animation work with interactive plot, so it blocks further execution of other code. That's why I could not use the animation module in my real-life application.

    import matplotlib
    matplotlib.use('TkAgg')
    import matplotlib.pyplot as plt
    import time
    from random import random
    
    print ( matplotlib.__version__ )
    
    # set up the figure
    plt.ion()
    fig = plt.figure()
    ax = plt.subplot(1,1,1)
    ax.set_xlabel('Time')
    ax.set_ylabel('Value')
    t = []
    y = []
    ax.plot( t , y , 'ko-' , markersize = 10 ) # add an empty line to the plot
    fig.show() # show the window (figure will be in foreground, but the user may move it to background)
    
    # plot things while new data is generated:
    # (avoid calling plt.show() and plt.pause() to prevent window popping to foreground)
    t0 = time.time()
    while True:
        t.append( time.time()-t0 )  # add new x data value
        y.append( random() )        # add new y data value
        ax.lines[0].set_data( t,y ) # set plot data
        ax.relim()                  # recompute the data limits
        ax.autoscale_view()         # automatic axis scaling
        fig.canvas.flush_events()   # update the plot and take care of window events (like resizing etc.)
        time.sleep(1)               # wait for next loop iteration
    
    0 讨论(0)
提交回复
热议问题