2

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.

Community
  • 1
  • 1
braunmagrin
  • 828
  • 6
  • 17
  • 1
    Don't use `--pylab` flag, it is deprecated. Use `%matplotlib` magic. Reason is to prevent memory leak we have to close the fig object in between cells. – Matt Mar 04 '15 at 18:27

2 Answers2

5

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.

cel
  • 30,017
  • 18
  • 97
  • 117
1

This happens because by default, the inline backend used in the IPython Notebook will close all matplotlib figures automatically after each cell is run. You can change this behavior via IPython.display.set_matplotlib_close as described here.