Why does savefig and plot commands have to be in the same cell in an IPython notebook?

和自甴很熟 提交于 2019-12-24 00:53:05

问题


I was trying to export some plots from a IPython notebook. Searching I've found this question and could sort the issue. As it's noted in the answer, I had to call savefig in the same cell as the plot commands.

My question is, why do those calls must be in the same cell? My notebook server was started in --pylab=inline mode. If it's not inline the plot are exported just fine.


回答1:


I think you are seeing the behavior from this part of IPython's code base:

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

After displaying the open figure all open plots are closed.



来源:https://stackoverflow.com/questions/28859965/why-does-savefig-and-plot-commands-have-to-be-in-the-same-cell-in-an-ipython-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!