How to keep matplotlib (python) window in background? How to keep matplotlib (python) window in background? python python

How to keep matplotlib (python) window in background?


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 matplotlibmatplotlib.use('TkAgg')import matplotlib.pyplot as pltimport timefrom random import randomprint ( matplotlib.__version__ )# set up the figureplt.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 plotfig.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


For the tkinter backend (matplotlib.use("TkAgg")), using flush_events is not sufficient: you also need to call fig.canvas.draw_idle() before each fig.canvas.flush_events(). As @samlaf wrote, the same holds for the Qt5Agg backend.