4

I am using the Spyder IDE and I find that matplotlib figure windows always get displayed behind other windows. 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. 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. I run into trouble with fig.canvas.manager.window. It says AttributeError: 'FigureManagerMac' object has no attribute 'window'.

4

1 回答 1

11

好吧,当我在做其他事情时,我碰巧找到了解决方案。

当我使用MacOSX后端时,然后fig.canvas.manager.window给出AttributeError: 'FigureManagerMac' object has no attribute 'window'. 但是,当我使用TkAgg后端时,则fig.canvas.manager具有属性window. 因此,我可以按如下方式实施此建议:

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)

很简单,对吧?第一个棘手的部分是您必须在导入 pyplot 之前设置后端。之后更改后端在我的经验中没有任何作用。第二个棘手的部分是 Spyder 的 Scientific Startup 脚本import matplotlib.pyplot as plt在您启动 Spyder IDE 时会正确运行,因此在导入 pyplot 之前您没有机会设置后端。解决这个问题的方法是转到 Preferences->Console->External Modules,将 GUI Backend 设置为 TkAgg,然后重新启动 Spyder。然后上面的代码就可以正常工作了。

以前我matplotlib.rcParams['backend'] = 'TkAgg'在启动 Spyder 后立即设置后端。但是,当我在做其他事情时,我开始收到提到MacOSX后端的错误。这对我来说没有任何意义,因为我认为我正在使用TkAgg. 令人抓狂的是当我查询matplotlib.get_backend它返回时TkAgg!显然,在导入 pyplot 之后设置后端就像您更改了后端一样,但实际上并没有更改后端。阿格!!

于 2013-11-17T22:53:51.753 回答