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'.

2
  • Had the same issue. Here is the solution: stackoverflow.com/a/32934536/5406821 Commented Oct 4, 2015 at 14:38
  • Looks like you need to set your SpyderPrefs to use Qt4Agg or some other non-MacOS backend for matplotlib. Works for me after doing this. Commented May 24, 2016 at 22:57

1 Answer 1

11

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!!

Sign up to request clarification or add additional context in comments.

1 Comment

(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 :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.