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?
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.
%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.
show() . If you do not want to use inline plotting, just use %matplotlib instead of %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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With