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?

1 Answer
1

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'. However, when I use the TkAgg backend, then fig.canvas.manager has the attribute 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. 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. The way around this is go to Preferences->Console->External Modules, set the GUI Backend to TkAgg, and restart Spyder. Then the code above works properly.

Previously I was setting the backend via matplotlib.rcParams['backend'] = 'TkAgg' right after launching Spyder. When I was doing something else, however, I started getting errors that mentioned the MacOSX backend. This did not make any sense to me, since I thought I was using TkAgg. The maddening part is when I queried matplotlib.get_backend it returned TkAgg! Apparently, setting the backend after importing pyplot acts as if you have changed the backend, but it does not actually change the backend. Argg!!

(Spyder dev here) I'm glad you figured out by yourself how to fix your problem. I have some words about matplotlib backends: on Mac we decided to set it to MacOSX irrespective of user preferences because it's the most reliable one, but given your confusion, I think we need to revisit this decision. I'll do it for our upcoming 2.3 release :)
–
Carlos CordobaNov 20 '13 at 14:43