使交互式matplotlib窗口在每次更新时都不会弹出到前面(Windows 7) [英] Make interactive matplotlib window not pop to front on each update (Windows 7)
问题描述
今天,在可能三年未升级的情况下,我已将matplotlib升级到了2.0.2版.
Today I upgraded matplotlib to version 2.0.2, after not upgrading for possibly 3 years.
现在,我遇到的问题是,在交互式绘图中,窗口始终位于最前面,就像这里
Now I have the problem that in interactive plots the window always comes to the front, as if this here make matplotlib plotting window pop up as the active one had come to be the default behaviour.
如何停用它?我不希望该窗口每5秒出现一次,并显示我的文本编辑器,浏览器,...
How can I deactivate it? I don't want the window to come to front every 5 seconds and raise over my text editor, browser, ...
我希望它保持在放置它的z顺序中,无论它在活动窗口的前面还是后面.
I want it to stay in the z-ordering where I've placed it, be it to front or behind an active window.
我认为,从2016年1月31日起,以下提交将导致该问题行为: tkagg:提高每个新窗口;部分地址为#596
I believe that the following commit from 31 Jan 2016 is responsible for this problematic behaviour: tkagg: raise each new window; partially addresses #596
在Github上找到了相关评论 https://github.com/matplotlib/matplotlib/issues/596#issuecomment-305298577
Found a related comment on Github https://github.com/matplotlib/matplotlib/issues/596#issuecomment-305298577
似乎是我对plt.pause
的呼叫导致了此问题,而不是plt.plot
呼叫.
it appears that it is my call to plt.pause
which is causing this issue, and not the plt.plot
call.
推荐答案
更改后端
该问题似乎仅在使用Tk
后端存在.使用Qt
后端,窗口将保持在使用plt.pause
更新时的位置.
Changing the backend
The issue seems only present using the Tk
backend. Using the Qt
backend, the window would stay where it was while updating with plt.pause
.
要更改后端,请在脚本开头使用这些行.
To change the backend use those lines at the beginning of your script.
import matplotlib
matplotlib.use("Qt4agg") # or "Qt5agg" depending on you version of Qt
修改plt.pause
如果不选择更改后端,则可能会有所帮助.窗口不断弹出到前面的原因是plt.pause
内部调用plt.show()
引起的.因此,您可以实现自己的pause
函数,而无需调用show
.这要求首先处于交互模式plt.ion()
,然后至少调用一次plt.show()
.然后,您可以使用自定义mypause
函数更新图,如下所示.
Modifying plt.pause
If changing the backend is not an option, the following might help. The cause of the window constantly popping up to the front comes from plt.pause
calling plt.show()
internally. You therefore implement you own pause
function, without calling show
. This requires to be in interactive mode plt.ion()
first and then at least once call plt.show()
. Afterwards you may update the plot with the custom mypause
function as shown below.
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
from time import time
from random import random
plt.ion()
# set up the figure
fig = plt.figure()
plt.xlabel('Time')
plt.ylabel('Value')
plt.show(block=False)
def mypause(interval):
backend = plt.rcParams['backend']
if backend in matplotlib.rcsetup.interactive_bk:
figManager = matplotlib._pylab_helpers.Gcf.get_active()
if figManager is not None:
canvas = figManager.canvas
if canvas.figure.stale:
canvas.draw()
canvas.start_event_loop(interval)
return
t0 = time()
t = []
y = []
while True:
t.append( time()-t0 )
y.append( random() )
plt.gca().clear()
plt.plot( t , y )
mypause(1)
使用animation
.
最后,使用matplotlib.animation
类将使以上所有内容都过时. matplotlib.animation.FuncAnimation
的示例在matplotlib页面上显示 .
Using an animation
.
Finally, using a matplotlib.animation
class would render all of the above obsolete. An example for matplotlib.animation.FuncAnimation
is shown on the matplotlib page.
这篇关于使交互式matplotlib窗口在每次更新时都不会弹出到前面(Windows 7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!