防止matplotlib提高数字

时间:2018-05-01 02:42:22

标签: linux python-3.x matplotlib

我最近升级到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时,奇怪的提升行为只发生在不多次执行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('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

1 个答案:

答案 0 :(得分:0)

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

需要做两件事:

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的参数是我们想要暂停的持续时间。

相关问题