Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib inline plots all figures after all text

In jupyter notebook, with %matplotlib inline enabled, the output of a cell that both prints text and plots a figure will have the entirety of the text appear before the figure is shown. This happens even if the figure was generated (and called show() on) before the text was printed.

For example:

fig = plt.figure()
fig.add_subplot(111)
fig.show()
print "hello"

will show the 'hello' before the empty figure.

How do I fix this so that each figure appears truly inline?

like image 552
unsorted Avatar asked Nov 02 '16 01:11

unsorted


People also ask

What happens if I dont use %Matplotlib inline?

In the current versions of the IPython notebook and jupyter notebook, it is not necessary to use the %matplotlib inline function. As, whether you call matplotlib. pyplot. show() function or not, the graph output will be displayed in any case.

Why is %Matplotlib inline?

%matplotlib inline sets the backend of matplotlib to the 'inline' backend: With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.

What can I use instead of %Matplotlib inline?

show() . If you do not want to use inline plotting, just use %matplotlib instead of %matplotlib inline .

Do We Still Need %Matplotlib inline?

So %matplotlib inline is only necessary to register this function so that it displays in the output. Running import matplotlib. pyplot as plt also registers this same function, so as of now it's not necessary to even use %matplotlib inline if you use pyplot or a library that imports pyplot like pandas or seaborn.


1 Answers

I think you want to explicitly display the figure, using IPython's display function:

from IPython.display import display

fig = plt.figure()
fig.add_subplot(111)
display(fig)
print("hello")
like image 177
minrk Avatar answered Oct 04 '22 19:10

minrk