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'
.
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!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With