How Do I Display A Matplotlib Figure Window On Top Of All Other Windows In Spyder
Solution 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!!
Post a Comment for "How Do I Display A Matplotlib Figure Window On Top Of All Other Windows In Spyder"