Plotting data with python and pylab

24
Barcelona Python Meetup Plotting data with python and pylab Giovanni M. Dall'Olio

description

A short introduction to how to read a CSV file and plot it with python and pylab.

Transcript of Plotting data with python and pylab

Page 1: Plotting data with python and pylab

Barcelona Python Meetup

Plotting data with python and pylab

Giovanni M. Dall'Olio

Page 2: Plotting data with python and pylab

Problem statement

Let's say we have a table of data like this: name country apples pears

Giovanni Italy 31 13Mario Italy 23 33Luigi Italy 0 5Margaret England 22 13Albert Germany 15 6

How to read it in python? How to do some basic plotting?

Page 3: Plotting data with python and pylab

Alternatives for plotting data in python

Pylab (enthought)  Matlab/Octave approach→

Enthought   extended version of Pylab→  (free for academic use)

rpy/rpy2   allows to run R commands within →python

Sage   interfaces python with Matlab, R, octave, →mathematica, ...

Page 4: Plotting data with python and pylab

The Pylab system

pylab is a system of three libraries, which together transform python in a Matlab­like environment

It is composed by: Numpy (arrays, matrices, complex numbers, etc.. in 

python) Scipy (extended scientific/statistics functions) Matplotlib (plotting library) iPython (extended interactive interpreter)

Page 5: Plotting data with python and pylab

How to install pylab

There are many alternatives to install PyLab: use the package manager of your linux distro  use enthought's distribution (

http://www.enthought.com/products/epd.php) (free for academic use)

compile and google for help!

Numpy and scipy contains some Fortran libraries, therefore easy_install doesn't work well with them

Page 6: Plotting data with python and pylab

ipython -pylab

Ipython is an extended version of the standard python interpreter

It has a modality especially designed for pylab The standard python interpreter doesn't support 

very well plotting (not multi­threading) So if you want an interactive interpreter, use 

ipython with the pylab option:

     $: alias pylab=”ipython -pylab” $: pylab

In [1]:

Page 7: Plotting data with python and pylab

Why the python interpreter is not the best for plotting

Gets blocked when you create a plot

Page 8: Plotting data with python and pylab

How to read a CSV file with python

To read a file like this in pylab:name country apples pearsGiovanni Italy 31 13Mario Italy 23 33Luigi Italy 0 5Margaret England 22 13Albert Germany 15 6

 → Use the function 'matplotlib.mlab.csv2rec'>>> data = csv2rec('exampledata.txt', delimiter='\t')

Page 9: Plotting data with python and pylab

Numpy - record arrays

csv2rec stores data in a numpy recarray object, where you can access columns and rows easily:

>>> print data['name'] ['Giovanni' 'Mario' 'Luigi' 'Margaret' 'Albert']

>>> data['apples'] array([31, 23, 0, 22, 15])

>>> data[1] ('Mario', 'Italy', 23, 33)

Page 10: Plotting data with python and pylab

Alternative to csv2rec

numpy.genfromtxt (new in 2009) More options than csv2rec, included in numpy Tricky default parameters: need to specify dtype=None

>>> data = numpy.genfromtxt('datafile.txt', dtype=None) >>> data array....

Page 11: Plotting data with python and pylab

Barchart

>>> data = csv2rec('exampledata.txt', delimiter='\t')

>>> bar(arange(len(data)), data['apples'], color='red', width=0.1, label='apples')

>>> bar(arange(len(data))+0.1, data['pears'], color='blue', width=0.1, label='pears')

>>> xticks(range(len(data)), data['name'], )

>>> legend()

>>> grid('.')

Page 12: Plotting data with python and pylab

Barchart

>>> data = csv2rec('exampledata.txt', delimiter='\t')

>>> figure() >>> clf()

Read a CSV file and storing it in a recordarray object

Use figure() and cls() to reset the graphic device

Page 13: Plotting data with python and pylab

Barchart

>>> data = csv2rec('exampledata.txt', delimiter='\t')

>>> bar(x=arange(len(data)), y=data['apples'], color='red', width=0.1, label='apples')

The bar function creates a barchart

Page 14: Plotting data with python and pylab

Barchart

>>> data = csv2rec('exampledata.txt', delimiter='\t')

>>> bar(x=arange(len(data)), y=data['apples'], color='red', width=0.1, label='apples')

>>> bar(arange(len(data))+0.1, data['pears'], color='blue', width=0.1, label='pears')

This is the second barchart

Page 15: Plotting data with python and pylab

Barchart

>>> data = csv2rec('exampledata.txt', delimiter='\t')

>>> bar(x=arange(len(data)), y=data['apples'], color='red', width=0.1, label='apples')

>>> bar(arange(len(data))+0.1, data['pears'], color='blue', width=0.1, label='pears')

>>> xticks(range(len(data)), data['name'], )

Re­defining the labels in the X axis (xticks)

Page 16: Plotting data with python and pylab

Barchart

>>> data = csv2rec('exampledata.txt', delimiter='\t')

>>> bar(x=arange(len(data)), y=data['apples'], color='red', width=0.1, label='apples')

>>> bar(arange(len(data))+0.1, data['pears'], color='blue', width=0.1, label='pears')

>>> xticks(range(len(data)), data['name'], )

>>> legend()>>> grid('.')>>> title('apples and pears by person')

Adding legend, grid, title

Page 17: Plotting data with python and pylab

Barchart (result)

Page 18: Plotting data with python and pylab

Pie Chart

>>> pie(data['pears'], labels=data['name'])

>>> pie(data['pears'], labels=['%s\n(%s pears)' % (i,j) for (i, j) in zip(data['name'], data['pears'])] )

Page 19: Plotting data with python and pylab

Pie chart (result)

Page 20: Plotting data with python and pylab

A plot chart >>> x = linspace(1,10, 10)>>> y = randn(10)>>> plot(x,y, 'r.', ms=15) 

Page 21: Plotting data with python and pylab

An histogram >>> x = randn(1000)>>> hist(x, bins=40)>>> title('histogram of random numbers') 

Page 22: Plotting data with python and pylab

Matplotlib gallery

Page 23: Plotting data with python and pylab

Scipy Cookbook

Page 24: Plotting data with python and pylab

Thanks for the attention!!

PyLab ­ http://www.scipy.org/PyLab  matplotlib ­ http://matplotlib.sourceforge.net/  scipy ­ http://www.scipy.org/  numpy ­ http://numpy.scipy.org/  ipython ­ http://ipython.scipy.org/moin/ 

These slides: http://bioinfoblog.it