如何在后台保持matplotlib(python)窗口? [英] How to keep matplotlib (python) window in background?

查看:128
本文介绍了如何在后台保持matplotlib(python)窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python/matplotlib应用程序,该应用程序经常使用来自测量仪器的新数据来更新绘图.当使用新数据更新绘图时,绘图窗口不应相对于桌面上的其他窗口从背景更改为前景(反之亦然).

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 background to foreground (or vice versa) with respect to other windows on my desktop when the plot is updated with new data.

这在运行带有matplotlib 1.5.2rc的Ubuntu 16.10的计算机上可以与Python 3配合使用.但是,在另一台装有Ubuntu 17.04和matplotlib 2.0.0的计算机上,每次使用新数据更新绘图时,图形窗口都会弹出到最前面.

This worked as desired with Python 3 on a machine running Ubuntu 16.10 with matplotlib 1.5.2rc. However, on a different machine with Ubuntu 17.04 and matplotlib 2.0.0, the figure window pops to the front every time the plot is updated with new data.

在使用新数据更新绘图时,如何控制窗口的前景/背景行为并保持窗口焦点?

How can I control the window foreground/background behavior and keep the window focus when updating the plot with new data?

下面是说明我的绘图例程的代码示例:

Here's a code example illustrating my plotting routine:

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)

推荐答案

matplotlib从1.5.2rc版本更改为2.0.0,以便pyplot.show()将窗口置于前台(请参见

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.

请注意,matplotlib动画模块可能是生成此示例中所示图的一个不错的选择.但是,我无法使动画与交互式绘图配合使用,因此它阻止了其他代码的进一步执行.这就是为什么我无法在现实生活中的应用程序中使用动画模块.

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

这篇关于如何在后台保持matplotlib(python)窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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