Zero
Zero

Reputation: 12099

Control IPython Notebook layout of plots and other output

When an IPython notebook has code in a cell like this:

display_html(HTML('Heading'))
plt.plot(x,y)
display_hmtl(HTML('Sub-heading'))
plt.plot(x,y)

The output from the plots is always collected at the end, like

Heading
Heading
[first plot]
[second plot]

How can the layout be controlled so the output is correctly interleaved?

(I know I can put the code in separate cells, but I want to generate long reports by calling blocks of code iteratively)

Upvotes: 0

Views: 387

Answers (1)

Rutger Kassies
Rutger Kassies

Reputation: 64443

You should also actively display the figures, instead of having IPython collecting the outputs and displaying it as a result. You should also close all active figures at the end, because for as far as i know, IPython will always display them (again) if they're still open.

For example:

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

Looks like:

enter image description here

Upvotes: 2

Related Questions