1. Basic Plotting with Pylab¶
 Download the full notebook [v2]
 Download the teaching notebook [v2]
Use the “v2” files in older versions of IPython, e.g. 0.12
Matplotlib Tutorial: 1. Basic Plot Interface
In this notebook, we will explore the basic plot interface using pylab.plot
and pylab.scatter
. We will also discuss the difference between the pylab interface
, which offers plotting with the feel of Matlab. In the following sections, we will introduce the objectoriented interface
, which offers more flexibility and will be used throughout the remainter of the tutorial.
Setting up IPython
IPython has a builtin mode to work cleanly with matplotlib figures. There are a few ways to invoke it:

On startup, you can add a command line argument:
ipython [notebook] pylab
or:
ipython notebook pylab inline
The first can be used with the notebook or with the normal IPython interpreter. The second specifies
that figures should be shown inline
, directly in the notebook. This is not available with the standard
IPython interpreter.

After starting IPython, the same can be accomplished through the
%pylab
magic command
:%pylab
or:
%pylab inline
The first works in either the interpreter or the notebook; the second should be used in the notebook. This is useful if an IPython session has already started.
We'll take the second route here, and tell IPython we want figures plotted inline:
%pylab inline
A first plot: the Pylab interface
Now we're ready for a plot. The %pylab
mode we entered above does a few things, among which is the
import of pylab
into the current namespace. For clarity, we'll do this directly here. We'll also
import numpy
in order to easily manipulate the arrays we'll plot:
import pylab
import numpy as np
Let's make some simple data to plot: a sinusoid
x = np.linspace(0, 20, 1000) # 100 evenlyspaced values from 0 to 50
y = np.sin(x)
pylab.plot(x, y)
Customizing the plot: Axes Limits
Let's play around with this a bit: first we can change the axis limits using xlim()
and ylim()
pylab.plot(x, y)
pylab.xlim(5, 15)
pylab.ylim(1.2, 1.2)
Customizing the plot: Axes Labels and Titles
We can label the axes and add a title:
pylab.plot(x, y)
pylab.xlabel('this is x!')
pylab.ylabel('this is y!')
pylab.title('My First Plot')
Labels can also be rendered using LaTeX symbols:
y = np.sin(2 * np.pi * x)
pylab.plot(x, y)
pylab.title(r'$\sin(2 \pi x)$') # the `r` before the string indicates a "raw string"
Customizing the plot: Line Styles
We can vary the line color or the line symbol:
pylab.plot(x, y, 'r') # solid red line ('r' comes from RGB color scheme)
pylab.xlim(0, 10)
pylab.ylim(1.2, 1.2)
pylab.xlabel('this is x!')
pylab.ylabel('this is y!')
pylab.title('My First Plot')
Other options for the color characters are:
'r' = red
'g' = green
'b' = blue
'c' = cyan
'm' = magenta
'y' = yellow
'k' = black
'w' = white
Options for line styles are
'' = solid
'' = dashed
':' = dotted
'.' = dotdashed
'.' = points
'o' = filled circles
'^' = filled triangles
and many, many more.
For more information, view the documentation of the plot function. In IPython, this can be
accomplished using the ?
functionality:
pylab.plot?
Also see the online version of this help: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot
Cusomizing the Plot: Legends
Multiple lines can be shown on the same plot. In this case, you can use a legend to label the two lines:
x = np.linspace(0, 20, 1000)
y1 = np.sin(x)
y2 = np.cos(x)
pylab.plot(x, y1, 'b', label='sine')
pylab.plot(x, y2, 'r', label='cosine')
pylab.legend(loc='upper right')
pylab.ylim(1.5, 2.0)
Exercise: Linestyles & Plot Customization
Below are two sets of arrays x1, y1
, and x2, y2
. Create a plot where
x1
and y1
are represented by blue circles, and x2
and y2
are
represented by a dotted black line. Label the symbols "sampled" and
"continuous", and add a legend. Adjust the y limits to suit your taste.
x1 = np.linspace(0, 10, 20)
y1 = np.sin(x1)
x2 = np.linspace(0, 10, 1000)
y2 = np.sin(x2)
pylab.plot(x1, y1, 'bo', label='sampled')
pylab.plot(x2, y2, ':k', label='continuous')
pylab.legend()
pylab.ylim(1.5, 2.0)