What is the pure Python equivalent to the IPython magic function call %matplotlib inline?

爷,独闯天下 提交于 2019-12-18 12:38:14

问题


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?


回答1:


%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 to get_ipython().magic('matplotlib inline') in which get_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.



来源:https://stackoverflow.com/questions/28724610/what-is-the-pure-python-equivalent-to-the-ipython-magic-function-call-matplotli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!