简体   繁体   English

如何在Spyder中的所有其他窗口的顶部显示matplotlib图形窗口

[英]How do I display a matplotlib figure window on top of all other windows in Spyder

I am using the Spyder IDE and I find that matplotlib figure windows always get displayed behind other windows. 我正在使用Spyder IDE,我发现matplotlib图形窗口总是显示在其他窗口后面。 For example, immediately after starting Spyder, if I type plt.plot([0,1],[0,1]) in the console, I get a plot behind the main Spyder window. 例如,在启动Spyder之后,如果我在控制台中键入plt.plot([0,1],[0,1]) ,我会在主Spyder窗口后面得到一个图。 How can I make new figure windows display on top off all other windows? 如何在所有其他窗口的顶部显示新的图形窗口?

I found this solution ( make matplotlib plotting window pop up as the active one ), but it does not work for me in Spyder. 我找到了这个解决方案( 使matplotlib绘图窗口弹出作为活动窗口 ),但它在Spyder中对我不起作用。 I run into trouble with fig.canvas.manager.window . 我遇到了fig.canvas.manager.window It says AttributeError: 'FigureManagerMac' object has no attribute 'window' . 它说AttributeError: 'FigureManagerMac' object has no attribute 'window'

Well, I happened upon the solution when I was working on something else. 好吧,当我在处理其他事情时,我碰巧遇到了解决方案。

When I use the MacOSX backend, then fig.canvas.manager.window gives AttributeError: 'FigureManagerMac' object has no attribute 'window' . 当我使用MacOSX后端时, fig.canvas.manager.window给出了AttributeError: 'FigureManagerMac' object has no attribute 'window' However, when I use the TkAgg backend, then fig.canvas.manager has the attribute window . 但是,当我使用TkAgg后端时, fig.canvas.manager具有属性window Thus, I can implement this suggestion as follows: 因此,我可以实现如下建议

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot([0,1],[0,1])
#Put figure window on top of all other windows
fig.canvas.manager.window.attributes('-topmost', 1)
#After placing figure window on top, allow other windows to be on top of it later
fig.canvas.manager.window.attributes('-topmost', 0)

Simple enough, right? 很简单吧? The first tricky part is you must set the backend before you import pyplot. 第一个棘手的部分是你必须在导入pyplot之前设置后端。 Changing the backend afterwards does nothing in my experience. 之后改变后端在我的经验中没有任何意义。 The second tricky part is Spyder's Scientific Startup script does import matplotlib.pyplot as plt right when you launch the Spyder IDE, so you have no chance to set the backend before pyplot is imported. 第二个棘手的部分是Spyder的Scientific Startup脚本确实在启动Spyder IDE时import matplotlib.pyplot as plt ,所以在导入pyplot之前你没有机会设置后端。 The way around this is go to Preferences->Console->External Modules, set the GUI Backend to TkAgg, and restart Spyder. 解决这个问题的方法是首选项 - >控制台 - >外部模块,将GUI后端设置为TkAgg,然后重新启动Spyder。 Then the code above works properly. 然后上面的代码正常工作。

Previously I was setting the backend via matplotlib.rcParams['backend'] = 'TkAgg' right after launching Spyder. 之前我在启动Spyder之后通过matplotlib.rcParams['backend'] = 'TkAgg'设置了后端。 When I was doing something else, however, I started getting errors that mentioned the MacOSX backend. 然而,当我做其他事情时,我开始收到提到MacOSX后端的错误。 This did not make any sense to me, since I thought I was using TkAgg . 这对我没有任何意义,因为我以为我在使用TkAgg The maddening part is when I queried matplotlib.get_backend it returned TkAgg ! 令人抓狂的部分是当我查询matplotlib.get_backend它返回了TkAgg Apparently, setting the backend after importing pyplot acts as if you have changed the backend, but it does not actually change the backend. 显然,在导入pyplot后设置后端就好像你已经改变了后端一样,但实际上并没有改变后端。 Argg!! ARGG!

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

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