How to make IPython notebook matplotlib plot inline
Solution 1
I used %matplotlib inline
in the first cell of the notebook and it works. I think you should try:
%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
You can also always start all your IPython kernels in inline mode by default by setting the following config options in your config files:
c.IPKernelApp.matplotlib=<CaselessStrEnum>
Default: None
Choices: ['auto', 'gtk', 'gtk3', 'inline', 'nbagg', 'notebook', 'osx', 'qt', 'qt4', 'qt5', 'tk', 'wx']
Configure matplotlib for interactive use with the default matplotlib backend.
Solution 2
If your matplotlib version is above 1.4, it is also possible to use
IPython 3.x and above
%matplotlib notebook
import matplotlib.pyplot as plt
older versions
%matplotlib nbagg
import matplotlib.pyplot as plt
Both will activate the nbagg backend, which enables interactivity.
Solution 3
Ctrl + Enter
%matplotlib inline
Magic Line :D
See: Plotting with Matplotlib.
Solution 4
Use the %pylab inline
magic command.
Solution 5
To make matplotlib inline by default in Jupyter (IPython 3):
Edit file
~/.ipython/profile_default/ipython_config.py
Add line
c.InteractiveShellApp.matplotlib = 'inline'
Please note that adding this line to ipython_notebook_config.py
would not work.
Otherwise it works well with Jupyter and IPython 3.1.0
Ian Fiske
Updated on July 19, 2022Comments
-
Ian Fiske almost 2 years
I am trying to use IPython notebook on MacOS X with Python 2.7.2 and IPython 1.1.0.
I cannot get matplotlib graphics to show up inline.
import matplotlib import numpy as np import matplotlib.pyplot as plt %matplotlib inline
I have also tried
%pylab inline
and the ipython command line arguments--pylab=inline
but this makes no difference.x = np.linspace(0, 3*np.pi, 500) plt.plot(x, np.sin(x**2)) plt.title('A simple chirp') plt.show()
Instead of inline graphics, I get this:
<matplotlib.figure.Figure at 0x110b9c450>
And
matplotlib.get_backend()
shows that I have the'module://IPython.kernel.zmq.pylab.backend_inline'
backend. -
mpavlov almost 10 yearsI would mark this as the right answer. The alternative
--pylab inline
works, but greets you with the following warning: Starting all kernels in pylab mode is not recommended, and will be disabled in a future release. Please use the %matplotlib magic to enable matplotlib instead. pylab implies many imports, which can have confusing side effects and harm the reproducibility of your notebooks. -
TSGM over 9 years@eNord9 @mightwolf: I'm learning to using iPython (and Python programming instead of Matlab); what does
import matplotlib' do versus
import matplotlib as [name]'? Forgive for simplistic comment -
TSGM over 9 years@eNord9 @mightwolf: and also how does this compare to `from matplotlib import mpl'.
-
mpavlov over 9 years@TSGM The best explanation I've seen for your question is: effbot.org/zone/import-confusion.htm
-
Ian Fiske almost 9 yearsThanks @eNord9. I just tested your commands since it's been a while with updates and all. Now everything works fine on Python 2.7.9 and IPython 3.1.0.
-
Dave X almost 9 yearsNo longer: "ipython notebook --pylab inline [E 15:01:18.182 NotebookApp] Support for specifying --pylab on the command line has been removed. [E 15:01:18.182 NotebookApp] Please use
%pylab inline
or%matplotlib inline
in the notebook itself." -
Dave X almost 9 yearsI needed to put the "%matplotlib inline" first in the cell of the block that had "import matplotlib" in it. If I put it in a separate first cell, it did not seem to apply to the import. Thanks.
-
William Stein almost 9 yearsRelevant thread: mail.scipy.org/pipermail/ipython-dev/2015-July/016631.html
-
CodeFarmer over 8 years@BradleyKreider Not dead yet. Alternatively, you can visit ipython repo on github, go into 'example' folder, find ' Plotting with Matplotlib' notebook.
-
WindChimes over 8 yearsFor the record, to undo this and switch back to the pop-up, you need to restart the notebook (and comment out the line in your code).
-
orome about 8 yearsThis doesn't seem to work with
%config InlineBackend.figure_format='retina'
. Any idea how to get interactive Retina figures? -
Løiten about 8 years
-
Hieu almost 8 yearsThis answer is underrated.
%matplotlib notebook
provides the better visualization than%matplotlib inline
. -
Ami almost 8 yearsActually, you may wish to edit this to say
%matplotlib notebook
-
michael over 7 yearsusing
%matplotlib notebook
does not work (kind of shows something, then blank) on jupyter notebook 4.1.1 / ubuntu 16.04 / chrome,%matplotlib inline
does show images, but they come after the markdown text, not literally "inline". -
Løiten over 7 yearsStrange, did you experience the same in jupyter notebook 4.3.0? I just checked, and it works good with notebook 4.3.0/ mint 18/chrome (Python 3.5.2, IPython 5.1.0).
-
Kyle Strand almost 7 yearsThis isn't a syntax error--without the
()
to invokekde
, iPython is telling you whatkde
is, namely, a bound method. So in fact from iPython's perspective, this is not an "error" at all, hence why there is no stack trace. -
Blake M almost 7 years@KyleStrand Thanks. After re-reading my post what I should have said is: "I thought I had the problem of not getting my plots to show inline using the
%matplotlib inline
command. Really I just forgot to put () on the end of the plot type. So if everything else fails, look at your parentheses for a mistake." -
Ben over 6 yearsWith notebook backend:
JavaScript output is disabled in JupyterLab
, have tolaunch Classic Notebook
-
tony_tiger about 6 yearsPlots with
%matplotlib notebook
can have axes or legends cut off: github.com/matplotlib/matplotlib/issues/9163 -
Czechnology about 6 yearsIf you tried
%matplotlib inline
first and then switch to%matplotlib notebook
, you might get empty result. Restart the kernel and run again.