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.
<matplotlib.figure.Figure at 0x110b9c450>
but <matplotlib.text.Text at 0x94f9320>
(because your last line is printing a title). Anyway, your code (with %matplotlib inline and plt.show()) works as expected on windows
%matplotlib inline
, the kernel stays busy permanently and I get no output. It has to be killed. This is trying to use the MacOSX
backend but I guess it cannot be opened for some reason. When not using ipython notebook, the MacOSX backend for matplotlib works just fine.
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.
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.
https://i.stack.imgur.com/u9YvH.png
%config InlineBackend.figure_format='retina'
. Any idea how to get interactive Retina figures?
%matplotlib notebook
provides the better visualization than %matplotlib inline
.
%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".
%matplotlib inline
first and then switch to %matplotlib notebook
, you might get empty result. Restart the kernel and run again.
Ctrl + Enter
%matplotlib inline
Magic Line :D
See: Plotting with Matplotlib.
Use the %pylab inline
magic command.
%pylab inline
or %matplotlib inline
in the notebook itself."
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
I have to agree with foobarbecue (I don't have enough recs to be able to simply insert a comment under his post):
It's now recommended that python notebook isn't started wit the argument --pylab
, and according to Fernando Perez (creator of ipythonnb) %matplotlib inline
should be the initial notebook command.
I found a workaround that is quite satisfactory. I installed Anaconda Python and this now works out of the box for me.
I did the anaconda install but matplotlib is not plotting
It starts plotting when i did this
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
I had the same problem when I was running the plotting commands in separate cells in Jupyter:
In [1]: %matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
In [2]: x = np.array([1, 3, 4])
y = np.array([1, 5, 3])
In [3]: fig = plt.figure()
<Figure size 432x288 with 0 Axes> #this might be the problem
In [4]: ax = fig.add_subplot(1, 1, 1)
In [5]: ax.scatter(x, y)
Out[5]: <matplotlib.collections.PathCollection at 0x12341234> # CAN'T SEE ANY PLOT :(
In [6]: plt.show() # STILL CAN'T SEE IT :(
The problem was solved by merging the plotting commands into a single cell:
In [1]: %matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
In [2]: x = np.array([1, 3, 4])
y = np.array([1, 5, 3])
In [3]: fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.scatter(x, y)
Out[3]: <matplotlib.collections.PathCollection at 0x12341234>
# AND HERE APPEARS THE PLOT AS DESIRED :)
You can simulate this problem with a syntax mistake, however, %matplotlib inline
won't resolve the issue.
First an example of the right way to create a plot. Everything works as expected with the imports and magic that eNord9 supplied.
df_randNumbers1 = pd.DataFrame(np.random.randint(0,100,size=(100, 6)), columns=list('ABCDEF'))
df_randNumbers1.ix[:,["A","B"]].plot.kde()
However, by leaving the ()
off the end of the plot type you receive a somewhat ambiguous non-error.
Erronious code:
df_randNumbers1.ix[:,["A","B"]].plot.kde
Example error:
<bound method FramePlotMethods.kde of <pandas.tools.plotting.FramePlotMethods object at 0x000001DDAF029588>>
Other than this one line message, there is no stack trace or other obvious reason to think you made a syntax error. The plot doesn't print.
()
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.
%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."
If you're using Jupyter notebooks in Visual Studio Code (VSCode) then the inline
backend doesn't seem to work so you need to specify widget
/ipympl
(which you may need to install support for e.g. pip install ipympl
):
%matplotlib widget
Success story sharing
--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.import matplotlib' do versus
import matplotlib as [name]'? Forgive for simplistic comment