r/Python • u/schnadamschnandler • Jan 10 '17
IPython shell with %matplotlib: the qt windows displaying figures disappear when I step away from the terminal application. Can this be fixed/is this typical behavior?
On my setup, figure windows created with the %matplotlib magic from an IPython shell (not entirely sure how this works) disappear after switching from my terminal application to any other running application, then back again. So, for example, if I am drawing a figure and want to check an /r/python thread in Chrome, after returning to my terminal application, I must re-declare the command plt.show().
Does this behavior sound atypical (something potentially wrong with my setup), or if it is typical, is there some possible workaround? Could this be unique to my installation/OS?
I am on MacOS Sierra, running the latest (just updated) versions of Anaconda and IPython. This problem is present using both iTerm2 and Terminal; does not seem to be related to the terminal application.
I tried asking in stackoverflow but they down voted my question without input, as is tradition.
View all comments
Show parent comment
1
u/flutefreak7 Jan 12 '17 edited Jan 12 '17
I keep meaning to look at this when I'm actually at my computer, but I'm in bed on my phone again... My guess is that in Qt console there is already a Qt event loop and that %matplotlib inline will use the Agg (non-gui) backend to render to temporary images which get rendered inline. If you want matplotlib figures with Qt console try %matplotlib qt (based on some quick Google searches...) or try just importing matplotlib then set the backend to Qt with matplotlib.use('Qt4agg') before importing pyplot.
http://matplotlib.org/faq/usage_faq.html#what-is-a-backend
Note that matplotlib is designed to use a single backend per session. Switching backends can work, but isn't strictly supported. The docs say it takes some black magic to do so (dumping loaded modules from memory and reloading?) and it's likely the %matplotlib magic is doing some thing more than calling use() because it can be used to switch between inline and gui.
Sorry if this isn't more help. Keep reading and looking for tutorials to do what you want. Try stack overflow (Nevermind, you already did... I upvoted it for you!) or r/learnpython or matplotlib forums, etc. Someone out there can help more than me I'm sure!