Python Plotting Beginners Guide
Python Plotting Beginners Guide
Sarah Blyth
July 23, 2009
Introduction
You need to have the following installed on your computer to be able to make nice
plots.
Python
Numpy - this is the module which does most array and mathematical manipulation
Matplotlib - this is the module you will be using for plotting
You can check these are installed by going to a terminal and typing:
$ python
>>> import numpy as np
>>> import pylab as pl
If you get no errors, then they are installed. If they are not installed, speak to the
department SysAdmin to help you install them.
You can also check which versions you have installed by doing:
$ python
>>> import numpy
>>> numpy.__version__
The output you get should look something like:
1.2.1
Typing:
>>> import matplotlib
>>> matplotlib.__version__
should give you something like:
0.98.5.2
Basic plots
Two basic plot types which you will find are used very often are (x,y) line and scatter
plots and histograms. Some code for making these two types of plots is included in
this section.
3.1
3.1.1
3.1.2
Scatter plots
Alternatively, you may want to plot quantities which have an x and y position. For
example, plotting the location of stars or galaxies in a field for example such as the
plot in Fig. 2. Try running the script below to do this:
****************************************************
# scatterplot.py
import numpy as np
import pylab as pl
#
x
#
y
3.2
3.2.1
It is very useful to be able to plot more than one set of data on the same axes and to
be able to differentiate between them by using different line and marker styles and
colours. You can specify the colour by inserting a 3rd parameter into the plot()
command. For example, in lineplot.py, try changing the line
pl.plot(x, y)
to
pl.plot(x, y, r)
This should give you the same line as before, but it should now be red.
The other colours you can easily use are:
character
b
g
r
c
m
y
k
w
color
blue
green
red
cyan
magenta
yellow
black
white
3.2.2
You can also change the style of the line e.g. to be dotted, dashed, etc. Try:
plot(x,y, --)
This should give you a dashed line now.
Other linestyles you can use can be found on the Matplotlib webpage http://
matplotlib.sourceforge.net/api/pyplot api.html#matplotlib.pyplot.plot.
3.2.3
Lastly, you can also vary the marker style you use. Try:
plot(x,y, b*)
This should give you blue star-shaped markers. The table below gives some more
options for setting marker types:
s
p
*
h
H
+
x
D
d
3.2.4
square marker
pentagon marker
star marker
hexagon1 marker
hexagon2 marker
plus marker
x marker
diamond marker
thin diamond marker
It is very important to always label the axes of plots to tell the viewer what they are
looking at. You can do this in python by using the commands:
pl.xlabel(put text here)
pl.ylabel(put text here)
You can make a title for your plot by:
pl.title(Put plot title here)
You can change the x and y ranges displayed on your plot by:
pl.xlim(x_low, x_high)
pl.ylim(y_low, y_high)
Have a look at the modified macro lineplotAxis.py below:
****************************************************
#lineplotAxis.py
import numpy as np
import pylab as pl
#
x
#
y
It is very easy to plot more than one plot on the same axes. You just need to define
the x and y arrays for each of your plots and then:
plot(x1, y1, r)
plot(x2, y2, g)
Check out this macro, lineplot2Plots.py and the resulting Fig. 4.
****************************************************
#lineplot2Plots.py
import numpy as np
import pylab as pl
6
3.2.6
Figure legends
Its very useful to add legends to plots to differentiate between the different lines or
quantities being plotted. In python you can make a legend as follows:
pl.legend((plot1, plot2), (label1, label2), best, numpoints=1)
The first parameter is a list of the plots you want labelled. The second parameter is
the list of labels. The third parameter is where you would like matplotlib to place
your legend. Other optiions are:
upper right, upper left, center, lower left, lower right.
best means that matplotlib will try to choose the position on your plot where the
legend will interfere least with what is plotted (i.e. avoid overlaps etc.).
Have a look at Fig. 5 which is made using the macro below:
****************************************************
# lineplotFigLegend.py
import numpy as np
import pylab as pl
# Make x, y arrays for each graph
x1 = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
x2 = [1, 2, 4, 6, 8]
y2 = [2, 4, 8, 12, 16]
8
3.3
Histograms
Histograms are very often used in science applications and it is highly likely that
you will need to plot them at some point! They are very useful to plot distributions
9
Figure 6:
histplot.py
Plot
made
with
Figure 7:
histplot.py
10
Plot
made
with
3.3.1
You can also set the width of your histogram bins yourself. Try adding the following
lines to the macro histplot.py and you should get the plot shown in Fig. 8.
bins = np.arange(-5., 16., 1.)
pl.hist(data, bins, histtype=stepfilled)
Matplotlib is reasonably flexible about allowing multiple plots per canvas and it is
easy to set this up. You need to first make a figure and then specify subplots as
follows:
fig1 = pl.figure(1)
pl.subplot(211)
subplot(211) means that you will have a figure with 2 rows, 1 column, and youre
going to draw in the top plot as shown in Fig. 9. If you want to plot something in
the lower section (as shown in Fig. 10), then you do:
pl.subplot(212)
You can play around with plotting a variety of layouts. For example, Fig. 11 is
created using the following commands:
11
f1 = pl.figure(1)
pl.subplot(221)
pl.subplot(222)
pl.subplot(212)
You can play with the sizes of the margins and gaps between the subplots using the
command:
pl.subplots_adjust(left=0.08, right=0.95, wspace=0.25, hspace=0.45)
12
Often you will have data in ascii (text) format which you will need to plot in some
way. In this section we will briefly discuss how to open, read from, and write to,
files.
5.1
There are many ways to read data from a file in python. Here I am going to illustrate one simple method, but you should read the Python, Numpy and Matplotlib
manuals to find out about the other ways which might sometimes apply better to
you particular situation.
You can use Numpy to read in numerical values from a text file. For example, lets
take the text file called fakedata.txt which contains the following data (2 columns
of numbers):
****************************************************
# fakedata.txt
0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
****************************************************
We can read this into a numpy 2D array and plot the second column against the
first using the macro readFileAndPlot.py and shown in Fig. 12:
****************************************************
#readFileAndPlot.py
import numpy as np
13
import pylab as pl
# Use numpy to load the data contained in the file
# fakedata.txt into a 2-D array called data
data = np.loadtxt(fakedata.txt)
# plot the first column as x, and second column as y
pl.plot(data[:,0], data[:,1], ro)
pl.xlabel(x)
pl.ylabel(y)
pl.xlim(0.0, 10.)
pl.show()
****************************************************
5.2
There are also various ways to write text to a text file. Here we show you one possible
way to do it.
You first have to open a file to write to, then write what you want to the file, and
then, do not forget to close the file! See the macro below, writeToFile.py to see
how to do this:
****************************************************
# writeToFile.py
14
import numpy as np
# Lets make 2 arrays (x, y) which we will write to a file
#
x
#
y
x
=
y
=
print x = , x
print y = , y
# Now open a file to write the data to
# w means open for writing
file = open(testdata.txt, w)
# loop over each line you want to write to
for i in range(len(x)):
# make a string for each line you want
# \t means tab
# \n means newline
# str() means you are converting the
txt = str(x[i]) + \t + str(y[i]) +
# write the txt to the file
file.write(txt)
file
to write
15