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

查看:102
本文介绍了如何将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() 将窗口带到前台(参见 这里).因此,关键是避免在循环中调用 pyplot.show().pyplot.pause() 也是如此.

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天全站免登陆