In IPython Notebook, I defined a function that contains a call to the magic function %matplotlib
, like this:
def foo(x):
%matplotlib inline
# ... some useful stuff happens in between here
imshow(np.asarray(img))
I'd like to put that function into a Python module so that I can just import and call it.
However, to do this, I'd need to remove the %matplotlib inline
from my code and replace it with its pure-Python equivalent.
What is the pure-Python equivalent?
%matplotlib inline
directly hook into IPython instance. You can get the equivalent by using%hist -t
that show you the processed input as pure python, which show that%matplotlib inline
is equivalent toget_ipython().magic('matplotlib inline')
in whichget_ipython()
return the current ipython shell object. It is pure python but will work only in an IPython kernel.For more in depth explanation,
%matplolib xxx
just set matplotlib backend to xxx, the case od inline is a bit different and requires first a backend which is shipped with IPython and not matplotlib. Even if this backend was in matplotlib itself, it needs hooks in IPython itself to trigger the display and GC of matplotlib figures after each cell execution.