linux - 防止 matplotlib 提高数字

标签 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

最佳答案

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

关于linux - 防止 matplotlib 提高数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50110738/

相关文章:

python - `return line, ` 和 `return line` 之间的区别

python - matplotlib 具有共享轴的子图

c - 在共享库中获取 undefined reference

sql - 将 SQL 结果传递到 shell 脚本中的数组时标识符无效

linux - 在 CentOS 上安装 Git 时遇到问题

python - 如何只提取 .tar.gz 成员的文件?

python - .在没有按我预期的方式运行之后

python-3.x - 我如何为维基百科页面构建一个基本的网络爬虫来收集链接?

python - Matplotlib 注释文本在轴外扩展

linux - 你能用 bash/sh 脚本 quine 写一个简单的每周提醒吗?