【问题标题】:What is the pure Python equivalent to the IPython magic function call %matplotlib inline?什么是纯 Python 等效于 IPython 魔术函数调用 %matplotlib inline?
【发布时间】:2015-04-27 18:38:39
【问题描述】:

在 IPython Notebook 中,我定义了一个函数,其中包含对魔术函数 %matplotlib 的调用,如下所示:

def foo(x):
    %matplotlib inline
    # ... some useful stuff happens in between here
    imshow(np.asarray(img))

我想将该函数放入 Python 模块中,以便我可以导入并调用它。

但是,要做到这一点,我需要从我的代码中删除 %matplotlib inline 并将其替换为纯 Python 等效项。

什么是纯 Python 等价物?

【问题讨论】:

  • pyplot.show 也许?不知道你在这里真正问的是什么
  • 你可以将你的 ipython notebook 设为 'ipython notebook --pylab inline'
  • 从不使用 --pylab 标志,它已被弃用并且不再起作用。

标签: python matplotlib ipython-notebook


【解决方案1】:

%matplotlib inline 直接挂钩到 IPython 实例。 您可以使用%hist -t 获得等价物,它将处理后的输入显示为纯python,这表明%matplotlib inline 等效于get_ipython().magic('matplotlib inline'),其中get_ipython() 返回当前的ipython shell 对象。它是纯 python,但只能在 IPython 内核中工作。

为了更深入的解释,%matplolib xxx 只需将 matplotlib 后端设置为 xxx,情况 od inline 有点不同,首先需要一个 IPython 附带的后端,而不是 matplotlib。即使这个后端在 matplotlib 本身中,它也需要 IPython 本身的钩子在每个单元格执行后触发 matplotlib 图形的显示和 GC。

【讨论】:

  • 嗨,我试过这种方法,但它返回了AttributeError: 'NoneType' object has no attribute 'magic'。有什么帮助吗?
【解决方案2】:

我试图注释掉这一行

get_ipython().run_line_magic('matplotlib', 'inline') 

它对我有用。

【讨论】:

  • 它是如何回答问题的?
猜你喜欢
  • 2016-10-28
  • 1970-01-01
  • 1970-01-01
  • 2015-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多