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

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

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

python大神给出的解决方案

我认为您正在从part的代码库的IPython中看到行为:

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')

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