plotting data with python and pylab
DESCRIPTION
A short introduction to how to read a CSV file and plot it with python and pylab.TRANSCRIPT
![Page 1: Plotting data with python and pylab](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/1.jpg)
Barcelona Python Meetup
Plotting data with python and pylab
Giovanni M. Dall'Olio
![Page 2: Plotting data with python and pylab](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/2.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/3.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/4.jpg)
The Pylab system
pylab is a system of three libraries, which together transform python in a Matlablike 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](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/5.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/6.jpg)
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 multithreading) 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](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/7.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/8.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/9.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/10.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/11.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/12.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/13.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/14.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/15.jpg)
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'], )
Redefining the labels in the X axis (xticks)
![Page 16: Plotting data with python and pylab](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/16.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/17.jpg)
Barchart (result)
![Page 18: Plotting data with python and pylab](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/18.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/19.jpg)
Pie chart (result)
![Page 20: Plotting data with python and pylab](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/20.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/21.jpg)
An histogram >>> x = randn(1000)>>> hist(x, bins=40)>>> title('histogram of random numbers')
![Page 22: Plotting data with python and pylab](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/22.jpg)
Matplotlib gallery
![Page 23: Plotting data with python and pylab](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/23.jpg)
Scipy Cookbook
![Page 24: Plotting data with python and pylab](https://reader033.vdocument.in/reader033/viewer/2022042515/554e8485b4c90573338b45e3/html5/thumbnails/24.jpg)
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