How can I keep a matplotlib (python) window in the background?

I have a python / matplotlib application that updates the plot frequently with new data coming from a meter. The plot window should not change from background to foreground (or vice versa) relative to other windows on my desktop when the plot is updated with new data.

This worked as desired with Python 3 on an Ubuntu 16.10 machine using matplotlib 1.5.2rc. However, on another machine with Ubuntu 17.04 and matplotlib 2.0.0, the numbers window appears in front every time the plot is updated with new data.

How can I control the foreground / background behavior of a window and keep the window focus when the chart is updated with new data?

Here's some sample code to illustrate my build scheme:

import matplotlib
import matplotlib.pyplot as plt
from time import time
from random import random

print ( matplotlib.__version__ )

# set up the figure
fig = plt.figure()
plt.xlabel('Time')
plt.ylabel('Value')
plt.ion()

# plot things while new data is generated:
t0 = time()
t = []
y = []
while True:
    t.append( time()-t0 )
    y.append( random() )
    fig.clear()
    plt.plot( t , y )
    plt.pause(1)

      

+3


source to share


1 answer


matplotlib was changed somewhere from 1.5.2rc to 2.0.0, so that pyplot.show () brings the window to the foreground (see here ). Therefore the key is to avoid calling pyplot.show()

in a loop. The same applies to pyplot.pause()

.

Below is a working example. This will still bring the window to the front at the start. But the user can move the window to the background and the window will stay there when the digit is updated with new data.



Note that the matplotlib animation module can be a good choice for creating the plot shown in this example. However, I couldn't get the animation to work with the interactive plot, so it blocks further execution of other code. So I couldn't use the animation module in my real 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


source







All Articles