什么是纯 Python 相当于 IPython 魔法函数调用 %matplotlib 内联?
What is the pure Python equivalent to the IPython magic function call %matplotlib inline?
在 IPython Notebook 中,我定义了一个包含对魔术函数 %matplotlib
的调用的函数,如下所示:
def foo(x):
%matplotlib inline
# ... some useful stuff happens in between here
imshow(np.asarray(img))
我想将该函数放入 Python 模块中,这样我就可以导入并调用它。
但是,要做到这一点,我需要从我的代码中删除 %matplotlib inline
并将其替换为它的纯 Python 等价物。
什么是纯Python等价物?
%matplotlib inline
直接挂接到 IPython 实例。
您可以通过使用 %hist -t
获得等效项,将处理后的输入显示为纯 python,这表明 %matplotlib inline
等效于 get_ipython().magic('matplotlib inline')
,其中 get_ipython()
return 当前 ipython shell 对象。它是纯 python 但只能在 IPython 内核中工作。
为了更深入的解释,%matplolib xxx
只需将 matplotlib 后端设置为 xxx,内联的情况有点不同,首先需要一个 IPython 而不是 matplotlib 附带的后端。即使这个后端在 matplotlib 本身中,它也需要 IPython 本身的钩子来在每次单元格执行后触发 matplotlib 图形的显示和 GC。
我试图注释掉这一行
get_ipython().run_line_magic('matplotlib', 'inline')
它对我有用。
在 IPython Notebook 中,我定义了一个包含对魔术函数 %matplotlib
的调用的函数,如下所示:
def foo(x):
%matplotlib inline
# ... some useful stuff happens in between here
imshow(np.asarray(img))
我想将该函数放入 Python 模块中,这样我就可以导入并调用它。
但是,要做到这一点,我需要从我的代码中删除 %matplotlib inline
并将其替换为它的纯 Python 等价物。
什么是纯Python等价物?
%matplotlib inline
直接挂接到 IPython 实例。
您可以通过使用 %hist -t
获得等效项,将处理后的输入显示为纯 python,这表明 %matplotlib inline
等效于 get_ipython().magic('matplotlib inline')
,其中 get_ipython()
return 当前 ipython shell 对象。它是纯 python 但只能在 IPython 内核中工作。
为了更深入的解释,%matplolib xxx
只需将 matplotlib 后端设置为 xxx,内联的情况有点不同,首先需要一个 IPython 而不是 matplotlib 附带的后端。即使这个后端在 matplotlib 本身中,它也需要 IPython 本身的钩子来在每次单元格执行后触发 matplotlib 图形的显示和 GC。
我试图注释掉这一行
get_ipython().run_line_magic('matplotlib', 'inline')
它对我有用。