为什么savefig和plot命令必须位于IPython Notebook的同一单元中?

bra*_*rin 2 python plot matplotlib ipython-notebook

我试图从IPython笔记本中导出一些图。搜索我已经找到了这个问题,可以对问题进行排序。正如答案中指出的那样,我必须savefig在与plot命令相同的单元格中进行调用。

我的问题是,为什么这些呼叫必须在同一单元格中?我的笔记本服务器以--pylab=inline模式启动。如果不是内联,则可以很好地导出绘图。

cel*_*cel 5

我想你看到从这个行为partIPython的代码库:

def show(close=None):
    """Show all figures as SVG/PNG payloads sent to the IPython clients.
    Parameters
    ----------
    close : bool, optional
      If true, a ``plt.close('all')`` call is automatically issued after
      sending all the figures. If this is set, the figures will entirely
      removed from the internal list of figures.
    """
    if close is None:
        close = InlineBackend.instance().close_figures
    try:
        for figure_manager in Gcf.get_all_fig_managers():
            display(figure_manager.canvas.figure)
    finally:
        show._to_draw = []
        # only call close('all') if any to close
        # close triggers gc.collect, which can be slow
        if close and Gcf.get_all_fig_managers():
            matplotlib.pyplot.close('all')
Run Code Online (Sandbox Code Playgroud)

显示打开的图形后,所有打开的图都将关闭。