13

假设我有一些数据,并且我想通过将其传递给自定义绘图函数 (myplot()) 来创建这些数据的绘图。我在 myplot() 中使用 matplotlib 的模块。

我希望 myplot() 将句柄返回到图形,而不是在调用此函数时显示图形。这是 iPython 的示例代码和输出。

在此处输入图像描述

我对此有两个问题:

  1. 为什么我仍然看到一个情节,即使我将 myplot() 的输出分配给 f?
  2. 当我将 myplot() 的输出分配给一个变量时,我需要什么来抑制这个图?
4

2 回答 2

8

启动ipython

ipython notebook

而不是

ipython notebook --pylab=inline

在此处输入图像描述

于 2013-07-09T14:08:39.533 回答
2

如果您不想以非内联方式启动整个笔记本,您可以使用以下代码:

%config InlineBackend.close_figures = False

def myplot(t,x):
    fig = figure()
    x = plot(t,x)
    fig.savefig('plot.png') # This is just to show the figure is still generated
    return fig

t = arange(0,6,0.01)
x = sin(t)

f = myplot(t,x)
于 2013-07-09T14:24:06.230 回答