繁体   English   中英

防止matplotlib提高数字

[英]Prevent matplotlib from raising figures

我最近升级到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的参数是我们要暂停的持续时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM