Matplotlib 控制IPython笔记本的绘图布局和其他输出

Matplotlib 控制IPython笔记本的绘图布局和其他输出,matplotlib,ipython-notebook,Matplotlib,Ipython Notebook,当IPython笔记本电脑的单元格中有这样的代码时: display_html(HTML('Heading')) plt.plot(x,y) display_hmtl(HTML('Sub-heading')) plt.plot(x,y) 绘图的输出总是在最后收集,如 Heading Heading [first plot] [second plot] 如何控制布局,使输出正确交错 (我知道我可以将代码放在单独的单元格中,但我希望通过迭代调用代码块来生成长报告)您还应该主动显示图形,而不是让I

当IPython笔记本电脑的单元格中有这样的代码时:

display_html(HTML('Heading'))
plt.plot(x,y)
display_hmtl(HTML('Sub-heading'))
plt.plot(x,y)
绘图的输出总是在最后收集,如

Heading
Heading
[first plot]
[second plot]
如何控制布局,使输出正确交错


(我知道我可以将代码放在单独的单元格中,但我希望通过迭代调用代码块来生成长报告)

您还应该主动显示图形,而不是让IPython收集输出并显示结果。你也应该在最后关闭所有活动的图形,因为据我所知,如果它们仍然打开,IPython将始终(再次)显示它们

例如:

display_html(HTML('Heading'))

fig, ax = plt.subplots()
ax.plot(np.arange(100),np.random.randn(100).cumsum())
display(fig)

display_html(HTML('Sub-heading'))

fig, ax = plt.subplots()
ax.plot(np.arange(100),np.random.randn(100).cumsum())
display(fig)

plt.close('all') # this closes all of them
看起来像: