为什么在IPython笔记本中,savefig和plot命令必须位于同一单元格中?
我试图从IPython笔记本中导出一些情节。我已经找到并可以对问题进行排序。正如答案中所指出的,我必须在为什么在IPython笔记本中,savefig和plot命令必须位于同一单元格中?,python,matplotlib,plot,ipython-notebook,Python,Matplotlib,Plot,Ipython Notebook,我试图从IPython笔记本中导出一些情节。我已经找到并可以对问题进行排序。正如答案中所指出的,我必须在plot命令所在的单元格中调用savefig 我的问题是,为什么这些电话必须在同一个牢房?我的笔记本服务器是在--pylab=inline模式下启动的。如果它不是内联的,则可以很好地导出绘图。我认为您可以从IPython的代码库中看到这种行为: def show(close=None): """Show all figures as SVG/PNG payloads sent to t
plot
命令所在的单元格中调用savefig
我的问题是,为什么这些电话必须在同一个牢房?我的笔记本服务器是在
--pylab=inline
模式下启动的。如果它不是内联的,则可以很好地导出绘图。我认为您可以从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')
显示打开的图形后,所有打开的绘图都将关闭。这是因为默认情况下,IPython笔记本中使用的内联后端将在每个单元格运行后自动关闭所有matplotlib图形。您可以通过
IPython.display.set\u matplotlib\u close
更改此行为,如前所述。注意,我不能完全确定,因此对此持保留态度。不要使用--pylab
标志,它已被弃用。使用%matplotlib
magic。原因是为了防止内存泄漏,我们必须关闭单元之间的fig对象。