防止 matplotlib 提高数字

问题描述 投票:0回答:2

我最近升级到了 matplotlib 2.1.1(我之前使用的是 1.5 左右)。现在,matplotlib 不断将图形窗口提升到前台。这曾经是新创建的窗口的情况(这是有道理的),但现在,只要在脚本中调用pause(),窗口就会被带到前台。

Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.use('TkAgg')
>>> import matplotlib.pyplot as plt
>>> plt.figure()
<matplotlib.figure.Figure object at 0x7fab8c0ace80>
>>> plt.pause(1)  # <--- the figure gets created and put into foreground here (this is expected)
>>> plt.pause(1)  # <--- the figure gets put into foreground again here (this is undesired)

奇怪的是,其他后端的行为略有变化。使用 qt5 时,只有当多次执行暂停()且中间没有交互式提示时才会发生奇怪的引发行为:

Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.use('Qt5Agg')
>>> import matplotlib.pyplot as plt
>>> plt.figure()
<matplotlib.figure.Figure object at 0x7fcaeff5de80>
>>> plt.pause(1)  # <--- the figure gets created and put into foreground here
>>> plt.pause(1)  # <--- the figure stays in background
>>> plt.pause(1); plt.pause(1) # <--- the figure gets put in foreground when the second pause is executed.

有人知道如何禁用此行为吗?我有一个经常更新数字并使用暂停的应用程序。随着人物一直在前景中弹出,计算机变得完全无法用于任何其他工作。

系统: Ubuntu 18.04(使用 Gnome 和 Unity 测试) Matplotlib 2.1.1 Python 3.6.5

linux python-3.x matplotlib
2个回答
1
投票

ngoldbaum 发布的链接和源代码显示了问题。暂停现在被设计为始终提高所有数字。基于此,我能够创建一个解决方法,避免使用暂停(),但允许我根据需要更新和暂停数字。

需要两件事:

1)每个新创建的图形都需要显示。否则,该窗口可能永远不会显示。因此,使用命令创建图形

figure = plt.figure()
figure.canvas.manager.show()

2)实际上,每当我不暂停时,就会执行不带 show() 的pause() 代码:

manager = matplotlib.backend_bases.Gcf.get_active()
if manager is not None:
    canvas = manager.canvas
    if canvas.figure.stale:
        canvas.draw_idle()
    canvas.start_event_loop(1)

这里,canvas.start_event_loop 的参数是我们要暂停的持续时间。


0
投票

现在可以作为内置选项使用:

import matplotlib
matplotlib.rcParams["figure.raise_window"] = False
© www.soinside.com 2019 - 2024. All rights reserved.