How to make IPython notebook matplotlib plot inline

1,232,043

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.

Example plot with the nbagg backend

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):

  1. Edit file ~/.ipython/profile_default/ipython_config.py

  2. 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

Share:
1,232,043
Ian Fiske
Author by

Ian Fiske

Updated on July 19, 2022

Comments

  • Ian Fiske
    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
    mpavlov almost 10 years
    I 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
    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
    TSGM over 9 years
    @eNord9 @mightwolf: and also how does this compare to `from matplotlib import mpl'.
  • mpavlov
    mpavlov over 9 years
    @TSGM The best explanation I've seen for your question is: effbot.org/zone/import-confusion.htm
  • Ian Fiske
    Ian Fiske almost 9 years
    Thanks @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
    Dave X almost 9 years
    No 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
    Dave X almost 9 years
    I 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
    William Stein almost 9 years
  • CodeFarmer
    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
    WindChimes over 8 years
    For 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
    orome about 8 years
    This doesn't seem to work with %config InlineBackend.figure_format='retina'. Any idea how to get interactive Retina figures?
  • Løiten
    Løiten about 8 years
    Hmm...I don't really have too much expertise with retina figures. The only thing I stumbled upon was this link, but it may be deprecated. If more people are wondering about the same, I link your SO question here, and wish you good luck with the answers there. Best
  • Hieu
    Hieu almost 8 years
    This answer is underrated. %matplotlib notebook provides the better visualization than %matplotlib inline.
  • Ami
    Ami almost 8 years
    Actually, you may wish to edit this to say %matplotlib notebook
  • michael
    michael over 7 years
    using %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
    Løiten over 7 years
    Strange, 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
    Kyle Strand almost 7 years
    This isn't a syntax error--without the () to invoke kde, iPython is telling you what kde 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
    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
    Ben over 6 years
    With notebook backend: JavaScript output is disabled in JupyterLab , have to launch Classic Notebook
  • tony_tiger
    tony_tiger about 6 years
    Plots with %matplotlib notebook can have axes or legends cut off: github.com/matplotlib/matplotlib/issues/9163
  • Czechnology
    Czechnology about 6 years
    If you tried %matplotlib inline first and then switch to %matplotlib notebook, you might get empty result. Restart the kernel and run again.