Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot() doesn't work on IPython notebook

I'm new to python scientific computing, and I tried to make a simple graph on IPython notebook.

import pandas
plot(arange(10))

Then error had shown as below.

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-6b139d572bd6> in <module>()
      1 import pandas
----> 2 plot(arange(10))

NameError: name 'plot' is not defined

Instead, with IPython --pylab mode, a right graph popped up when I tried the same code.

Am I missing any environment?

My environment is Mac OSX 10.8.5, python 2.7.5, IPython 1.1.0, matplotlib 1.3.1, and pandas 0.12.0. I downloaded python scientific environment by Anaconda installer from continuum.io. Anaconda version is the newest one as of 1/30/2014.

like image 804
mako Avatar asked Jan 30 '14 13:01

mako


People also ask

How do you show plot in Jupyter notebook?

Usually, displaying plots involves using the show() function from PyPlot. With Jupyter notebooks, this isn't necessary as the plots are displayed after running the cells containing the code that generates them. These plots are by default, displayed inline, which means, they're displayed in the notebook itself.

What does plot () do in Python?

The plot() function is used to draw points (markers) in a diagram. By default, the plot() function draws a line from point to point. The function takes parameters for specifying points in the diagram.

Can you use matplotlib in Jupyter notebook?

Matplotlib is a Python library that is used often with Jupyter Notebook.


1 Answers

It is not advisable to use pylab mode. See the following post from Matthias Bussonnier

A summary from that post:

Why not to use pylab flag:

  1. It is irreversible- Cannot unimport
  2. Unclear- if someone else did not run with this flag (or with a different setting of it) what would happen?
  3. Pollutes the namespace
  4. Replaces built-ins
  5. Side effects

You are much better by doing the following inside your IPython notebook.

%matplotlib inline

import matplotlib.pyplot as plt
plt.plot(range(10))

The following is the code which --pylab brings into the namespace

import numpy
import matplotlib
from matplotlib import pylab, mlab, pyplot
np = numpy
plt = pyplot

from IPython.core.pylabtools import figsize, getfigs

from pylab import *
from numpy import *

Still, if you wish to use pylab and have plots inline, you may do either of the following:

From shell:

$ ipython notebook --pylab inline

Or, from within your notebook

%pylab inline
like image 61
Nipun Batra Avatar answered Oct 11 '22 12:10

Nipun Batra