Introduzione a AstroInformatics Python - unina.itdame.dsf.unina.it/ASTROINFOEDU/L5-Python.pdf ·...

Preview:

Citation preview

Introduzione a Python

AstroInformatics

Stefano Cavuoti

Python

◼ General purpose interpreted programming language

◼ Widely used by scientists and programmers of all stripes

◼ Supported by many 3rd-party libraries (currently 21,054 on the main python package website)

◼ Free!

Snape

Why is it well-suited to science?

◼ Numpy▪ Numerical library for python▪ Written in C, wrapped by python▪ Fast

◼ Scipy▪ Built on top of numpy (i.e. Also fast!)▪ Common maths, science, engineering routines

◼ Matplotlib▪ Hugely flexible plotting library▪ Similar syntax to Matlab▪ Produces publication-quality output

▪ Pyfits▪ Handle fits files

▪ Scikit-learn▪ Several machine learning methods

▪ …

◼ An integrated graphical environment like Matlab (although there are tools which put it in one – e.g. Spyder)

◼ Specifically designed for scientists/mathematicians (but the 3rd-party libraries for plotting/numerical work are some of the best around)

◼ High performance (but it is very easy to wrap C/Fortran libraries in Python code)

What is Python not?

Who uses Python?

• Netflix • Yahoo Maps/Groups• Google• NASA• ESRI• Linux distros• Multiplayer.it• CIA• Civilization 4

Who uses Python?

• Stefano Cavuoti

• Netflix • Yahoo Maps/Groups• Google• NASA• ESRI• Linux distros• Multiplayer.it• CIA• Civilization 4

Something cool that Python can do.

◼ It can do everything▪ Fast mathematical operations

▪ Easy file manipulation

▪ Format conversion

▪ Plotting

▪ Scripting

▪ Command line

Something cool that Python can do.

◼ It can do everything▪ Fast mathematical operations

▪ Easy file manipulation

▪ Format conversion

▪ Plotting

▪ Scripting

▪ Command line

◼ OK, not everything▪ Write papers for you

◼ Windows – Python(x,y) [www.pythonxy.com]This is a scientific/engineering oriented distribution of python. It includes everything you need to get started

◼ Linux – it’s already there! Unless you’re running a very unusual distro (in which case you probably already know what you’re doing).

◼ Mac – it’s already there on OS X, but it’s old. Get a more up-to-date one [www.python.org]

How can I get Python?

◼ The official python tutorial:http://docs.python.org/tutorial/

◼ Software Carpentry:http://software-carpentry.org/

◼ Dive into Python:http://www.diveintopython.net/

◼ Learn Python the Hard Way:http://learnpythonthehardway.org/

◼ A Byte of Python:http://www.ibiblio.org/g2swap/byteofpython/read/

◼ Pensare da informatico: http://www.python.it/doc/Howtothink/HowToThink_ITA.pdf

How can I learn Python?

• Extra features required:– fast, multidimensional arrays

– libraries of reliable, tested scientific functions

– plotting tools• NumPy is at the core of nearly every scientific Python

application or module since it provides a fast N-d array datatype that can be manipulated in a vectorized form.

Scientific Python?

Hello World

>>> 'hello world!'

'hello world!'

•Open a terminal window and type “python”

•If on Windows open a Python IDE like IDLE

•At the prompt type ‘hello world!’

Python Overview

From Learning Python, 2nd Edition:

• Programs are composed of modules• Modules contain statements• Statements contain expressions• Expressions create and process objects

The Python Interpreter

•Python is an interpreted language

•The interpreter provides an interactive environment to play with the language

•Results of expressions are printed on the screen

>>> 3 + 710>>> 3 < 15 True>>> 'print me''print me'>>> print 'print me'print me>>>

The print Statement

>>> print 'hello'hello>>> print 'hello', 'there'hello there

•Elements separated by commas print with a space between them•A comma at the end of the statement (print ‘hello’,) will not print a newline character

Documentation

>>> 'this will print'

'this will print'

>>> #'this will not'

>>>

The ‘#’ starts a line comment

Variables

• Are not declared, just assigned• The variable is created the first time you assign it

a value• Are references to objects• Type information is with the object, not the

reference• Everything in Python is an object

Everything is an object

• Everything means everything, including functions and classes (more on this later!)

• Data type is a property of the object and not of the variable

>>> x = 7>>> x7>>> x = 'hello'>>> x'hello'>>>

Numbers: Integers

• Integer – the equivalent of a C long

• Long Integer – an unbounded integer value.

>>> 132224132224>>> 132323 ** 217509376329L>>>

Numbers: Floating Point• int(x) converts x to an

integer• float(x) converts x to a

floating point• The interpreter shows

a lot of digits

>>> 1.232321.2323200000000001>>> print 1.232321.23232>>> 1.3E713000000.0>>> int(2.0)2>>> float(2)2.0

Numbers: Complex

• Built into Python• Same operations are

supported as integer and float

>>> x = 3 + 2j>>> y = -1j>>> x + y(3+1j)>>> x * y(2-3j)

Numbers are immutable

>>> x = 4.5>>> y = x>>> y += 3>>> x4.5>>> y7.5

x 4.5

y

x 4.5y 7.5

String Literals

• Strings are immutable• There is no char type like

in C++ or Java• + is overloaded to do

concatenation

>>> x = 'hello'>>> x = x + ' there'>>> x'hello there'

String Literals: Many Kinds• Can use single or double quotes, and three double

quotes for a multi-line string

>>> 'I am a string''I am a string'>>> "So am I!"'So am I!'>>> s = """And me too!though I am much longer than the others :)"""'And me too!\nthough I am much longer\nthan the others :)‘>>> print sAnd me too!though I am much longerthan the others :)‘

Substrings and Methods>>> s = '012345'>>> s[3]'3'>>> s[1:4]'123'>>> s[2:]'2345'>>> s[:4]'0123'>>> s[-2]'4'

• len(String) – returns the number of characters in the String

• str(Object) – returns a String representation of the Object

>>> len(x)6>>> str(10.3)'10.3'

String Formatting

• Similar to C’s printf• <formatted string> % <elements to insert>• Can usually just use %s for everything, it will

convert the object to its String representation.

>>> "One, %d, three" % 2'One, 2, three'>>> "%d, two, %s" % (1,3)'1, two, 3'>>> "%s two %s" % (1, 'three')'1 two three'>>>

Split and Join>>> s = 'a,b,c,d'>>> b=s.split(‘,’)>>> b[‘a’,’b’,’c’,’d’]>>> “ “.join(b)“a b c d”

• String.split(separator) – returns a list of the pieces of the original string

• String.join(List) – returns a String obtained joining the pieces of the list (if all of them are strings)

Lists

• Ordered collection of data• Data can be of different

types• Lists are mutable• Issues with shared

references and mutability• Same subset operations as

Strings

>>> x = [1,'hello', (3 + 2j)]>>> x[1, 'hello', (3+2j)]>>> x[2](3+2j)>>> x[0:2][1, 'hello']

Lists: Modifying Content

• x[i] = a reassigns the ith element to the value a

• Since x and y point to the same list object, both are changed

• The method append also modifies the list

>>> x = [1,2,3]>>> y = x>>> x[1] = 15>>> x[1, 15, 3]>>> y[1, 15, 3]>>> x.append(12)>>> y[1, 15, 3, 12]

Lists: Modifying Contents

• The method append modifies the list and returns None

• List addition (+) returns a new list

>>> x = [1,2,3]>>> y = x>>> z = x.append(12)>>> z == NoneTrue>>> y[1, 2, 3, 12]>>> x = x + [9,10]>>> x[1, 2, 3, 12, 9, 10]>>> y[1, 2, 3, 12]>>>

Tuples

• Tuples are immutable versions of lists

• One strange point is the format to make a tuple with one element:‘,’ is needed to

differentiate from the mathematical expression (2)

>>> x = (1,2,3)>>> x[1:](2, 3)>>> y = (2,)>>> y(2,)>>>

Dictionaries

• A set of key-value pairs• Dictionaries are mutable

>>> d = {1 : 'hello', 'two' : 42, 'blah' : [1,2,3]}>>> d{1: 'hello', 'two': 42, 'blah': [1, 2, 3]}>>> d['blah'][1, 2, 3]

Dictionaries: Add/Modify

>>> d{1: 'hello', 'two': 42, 'blah': [1, 2, 3]}>>> d['two'] = 99>>> d{1: 'hello', 'two': 99, 'blah': [1, 2, 3]}

>>> d[7] = 'new entry' >>> d{1: 'hello', 7: 'new entry', 'two': 99, 'blah': [1, 2, 3]}

• Entries can be changed by assigning to that entry

• Assigning to a key that does not exist adds an entry

Dictionaries: Deleting Elements

• The del method deletes an element from a dictionary

>>> d{1: 'hello', 2: 'there', 10: 'world'}>>> del(d[2])>>> d{1: 'hello', 10: 'world'}

Copying Dictionaries and Lists

• The built-in list function will copy a list

• The dictionary has a method called copy

>>> l1 = [1]>>> l2 = list(l1)>>> l1[0] = 22>>> l1[22]>>> l2[1]

>>> d = {1 : 10}>>> d2 = d.copy()>>> d[1] = 22>>> d{1: 22}>>> d2{1: 10}

Data Type Summary

• Lists, Tuples, and Dictionaries can store any type (including other lists, tuples, and dictionaries!)

• Only lists and dictionaries are mutable• All variables are references

Data Type Summary

• Integers: 2323, 3234L• Floating Point: 32.3, 3.1E2• Complex: 3 + 2j, 1j• Lists: l = [ 1,2,3]• Tuples: t = (1,2,3)• Dictionaries: d = {‘hello’ : ‘there’, 2 : 15}

Input

• The raw_input(string) method returns a line of user input as a string

• The parameter is used as a prompt• The string can be converted by using the

conversion methods int(string), float(string), etc.

Input: Exampleprint "What's your name?"name = raw_input("> ")

print "What year were you born?"birthyear = int(raw_input("> "))

print "Hi %s! You are %d years old!" % (name, 2011 - birthyear)

~: python input.pyWhat's your name?> MichaelWhat year were you born?>1980Hi Michael! You are 31 years old!

Files: Inputinflobj = open(‘data’, ‘r’) Open the file ‘data’ for

input

S = inflobj.read() Read whole file into one String

S = inflobj.read(N) Reads N bytes (N >= 1)

L = inflobj.readlines() Returns a list of line strings

Files: Outputoutflobj = open(‘data’, ‘w’) Open the file ‘data’ for

writing

outflobj.write(S) Writes the string S to file

outflobj.writelines(L) Writes each of the strings in list L to file

outflobj.close() Closes the file

Booleans

• 0 and None are false• Everything else is true• True and False are aliases for 1 and 0 respectively

Boolean Expressions

• Compound boolean expressions short circuit

• and and or return one of the elements in the expression

• Note that when None is returned the interpreter does not print anything

>>> True and FalseFalse>>> False or TrueTrue>>> 7 and 1414>>> None and 2>>> None or 22

Moving to Files

• The interpreter is a good place to try out some code, but what you type is not reusable

• Python code files can be read into the interpreter using the import statement

Moving to Files• In order to be able to find a module called myscripts.py,

the interpreter scans the list sys.path of directory names.• The module must be in one of those directories.

>>> import sys>>> sys.path['C:\\Python26\\Lib\\idlelib', 'C:\\WINDOWS\\system32\\python26.zip', 'C:\\Python26\\DLLs', 'C:\\Python26\\lib', 'C:\\Python26\\lib\\plat-win', 'C:\\Python26\\lib\\lib-tk', 'C:\\Python26', 'C:\\Python26\\lib\\site-packages']>>> import myscriptsTraceback (most recent call last): File "<pyshell#2>", line 1, in <module> import myscripts.pyImportError: No module named myscripts.py

No Braces

• Python uses indentation instead of braces to determine the scope of expressions

• All lines must be indented the same amount to be part of the scope (or indented more if part of an inner scope)

• This forces the programmer to use proper indentation since the indenting is part of the program!

If Statementsimport math x = 30 if x <= 15 : y = x + 15elif x <= 30 : y = x + 30else : y = xprint ‘y = ‘,print math.sin(y)

In file ifstatement.py

>>> import ifstatementy = 0.999911860107>>>

In interpreter

While Loops

x = 1while x < 10 : print x x = x + 1

>>> import whileloop123456789>>>

In whileloop.py

In interpreter

Loop Control Statements

break Jumps out of the closest enclosing loop

continue Jumps to the top of the closest enclosing loop

pass Does nothing, empty statement placeholder

The Loop Else Clause

• The optional else clause runs only if the loop exits normally (not by break)

x = 1

while x < 3 : print x x = x + 1else: print 'hello'

~: python whileelse.py 12hello

Run from the command line

In whileelse.py

The Loop Else Clause

x = 1while x < 5 : print x x = x + 1 breakelse : print 'i got here'

~: python whileelse2.py 1

whileelse2.py

For Loops• Similar to perl for loops, iterating through a list of values

~: python forloop1.py 17132

for x in [1,7,13,2] : print xforloop1.py

~: python forloop2.py 01234

for x in range(5) : print xforloop2.py

range(N) generates a list of numbers [0,1, …, n-1]

For Loops• For loops also may have the optional else clause

for x in range(5): print x breakelse : print 'i got here'

~: python elseforloop.py 1

elseforloop.py

Function Basics

def max(x,y) : if x < y : return x else : return y

>>> import functionbasics>>> max(3,5)5>>> max('hello', 'there')'there'>>> max(3, 'hello')'hello'

functionbasics.py

Functions are first class objects

• Can be assigned to a variable• Can be passed as a parameter• Can be returned from a function• Functions are treated like any other variable in

Python, the def statement simply assigns a function to a variable

Function names are like any variable

• Functions are objects• The same reference

rules hold for them as for other objects

>>> x = 10>>> x10>>> def x () : ... print 'hello'>>> x<function x at 0x619f0>>>> x()hello>>> x = 'blah'>>> x'blah'

Functions as Parameters

def foo(f, a) : return f(a)

def bar(x) : return x * x

>>> from funcasparam import *>>> foo(bar, 3)9

Note that the function foo takes two parameters and applies the first as a function with the second as its parameter

funcasparam.py

Higher-Order Functions map(func,seq) – for all i, applies func(seq[i]) and returns the corresponding sequence of the calculated results.

def double(x): return 2*x

>>> from highorder import *>>> lst = range(10)>>> lst [0,1,2,3,4,5,6,7,8,9]>>> map(double,lst)[0,2,4,6,8,10,12,14,16,18]

highorder.py

Higher-Order Functions

filter(boolfunc,seq) – returns a sequence containing all those items in seq for which boolfunc is True.

def even(x): return ((x%2 == 0)

>>> from highorder import *>>> lst = range(10)>>> lst [0,1,2,3,4,5,6,7,8,9]>>> filter(even,lst)[0,2,4,6,8]

highorder.py

Higher-Order Functions

reduce(func,seq) – applies func to the items of seq, from left to right, two-at-time, to reduce the seq to a single value.

def plus(x,y): return (x + y)

>>> from highorder import *>>> lst = [‘h’,’e’,’l’,’l’,’o’]>>> reduce(plus,lst)‘hello’

highorder.py

Functions Inside Functions

• Since they are like any other object, you can have functions inside functions

def foo (x,y) : def bar (z) : return z * 2 return bar(x) + y

>>> from funcinfunc import *>>> foo(2,3)7

funcinfunc.py

Functions Returning Functions

def foo (x) : def bar(y) : return x + y return bar# mainf = foo(3)print fprint f(2)

~: python funcreturnfunc.py<function bar at 0x612b0>5

funcreturnfunc.py

Parameters: Defaults

• Parameters can be assigned default values

• They are overridden if a parameter is given for them

• The type of the default doesn’t limit the type of a parameter

>>> def foo(x = 3) :... print x... >>> foo()3>>> foo(10)10>>> foo('hello')hello

Parameters: Named

• Call by name• Any positional

arguments must come before named ones in a call

>>> def foo (a,b,c) :... print a, b, c... >>> foo(c = 10, a = 2, b = 14)2 14 10>>> foo(3, c = 2, b = 19)3 19 2

Anonymous Functions

• A lambda expression returns a function object

• The body can only be a simple expression, not complex statements

>>> f = lambda x,y : x + y>>> f(2,3)5>>> lst = ['one', lambda x : x * x, 3]>>> lst[1](4)16

Modules

• The highest level structure of Python• Each file with the py suffix is a module• Each module has its own namespace

Modules: Importsimport mymodule Brings all elements of

mymodule in, but must refer to as mymodule.<elem>

from mymodule import x Imports x from mymodule right into this namespace

from mymodule import * Imports all elements of mymodule into this namespace

• Lists ok for storing small amounts of one-dimensional data

• But, can’t use directly with arithmetical operators (+, -, *, /, …)• Need efficient arrays with arithmetic and better multidimensional

tools• Numpy• Similar to lists, but much more capable, except fixed size

>>> a = [1,3,5,7,9]

>>> print(a[2:4])

[5, 7]

>>> b = [[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]

>>> print(b[0])

[1, 3, 5, 7, 9]

>>> print(b[1][2:4])

[6, 8]

>>> import numpy

>>> a = [1,3,5,7,9]

>>> b = [3,5,6,7,9]

>>> c = a + b

>>> print c

[1, 3, 5, 7, 9, 3, 5, 6, 7, 9]

Arrays – Numerical Python (Numpy)

The fundamental library needed for scientific computing with Python is called NumPy. This Open Source library contains:• a powerful N-dimensional array object• advanced array slicing methods (to select array elements)• convenient array reshaping methods

and it even contains 3 libraries with numerical routines:• basic linear algebra functions• basic Fourier transforms• sophisticated random number capabilities

NumPy can be extended with C-code for functions where performance is highly time critical. In addition, tools are provided for integrating existing Fortran code. NumPy is a hybrid of the older NumArray and Numeric packages, and is meant to replace them both.

Numpy – N-dimensional Array manipulations

• There are a number of ways to initialize new numpy arrays, for example from – a Python list or tuples – using functions that are dedicated to generating numpy arrays, such as

arange, linspace, etc. – reading data from files

Numpy – Creating arrays

• From lists– numpy.array

# as vectors from lists

>>> a = numpy.array([1,3,5,7,9])

>>> b = numpy.array([3,5,6,7,9])

>>> c = a + b

>>> print c

[4, 8, 11, 14, 18]

>>> type(c)

(<type 'numpy.ndarray'>)

>>> c.shape

(5,)

Numpy – Creating vectors

>>> l = [[1, 2, 3], [3, 6, 9], [2, 4, 6]] # create a list

>>> a = numpy.array(l) # convert a list to an array

>>>print(a)

[[1 2 3]

[3 6 9]

[2 4 6]]

>>> a.shape

(3, 3)

>>> print(a.dtype) # get type of an array

int64

# or directly as matrix

>>> M = array([[1, 2], [3, 4]])

>>> M.shape

(2,2)

>>> M.dtype

dtype('int64')

Numpy – Creating matrices

>>> l = [[1, 2, 3], [3, 6, 9], [2, 4, 6]] # create a list

>>> a = numpy.array(l) # convert a list to an array

>>>print(a)

[[1 2 3]

[3 6 9]

[2 4 6]]

>>> a.shape

(3, 3)

>>> print(a.dtype) # get type of an array

int64

# or directly as matrix

>>> M = array([[1, 2], [3, 4]])

>>> M.shape

(2,2)

>>> M.dtype

dtype('int64')

#only one type

>>> M[0,0] = "hello"

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

ValueError: invalid literal for long() with base 10: 'hello‘

>>> M = numpy.array([[1, 2], [3, 4]], dtype=complex)

>>> M

array([[ 1.+0.j, 2.+0.j],

[ 3.+0.j, 4.+0.j]])

Numpy – Creating matrices

>>> print(a)

[[1 2 3]

[3 6 9]

[2 4 6]]

>>> print(a[0]) # this is just like a list of lists

[1 2 3]

>>> print(a[1, 2]) # arrays can be given comma separated indices

9

>>> print(a[1, 1:3]) # and slices

[6 9]

>>> print(a[:,1])

[2 6 4]

>>> a[1, 2] = 7

>>> print(a)

[[1 2 3]

[3 6 7]

[2 4 6]]

>>> a[:, 0] = [0, 9, 8]

>>> print(a)

[[0 2 3]

[9 6 7]

[8 4 6]]

Numpy – Matrices use

• Generation functions>>> x = arange(0, 10, 1) # arguments: start, stop, step

>>> x

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

>>> numpy.linspace(0, 10, 25)

array([ 0. , 0.41666667, 0.83333333, 1.25 ,

1.66666667, 2.08333333, 2.5 , 2.91666667,

3.33333333, 3.75 , 4.16666667, 4.58333333,

5. , 5.41666667, 5.83333333, 6.25 ,

6.66666667, 7.08333333, 7.5 , 7.91666667,

8.33333333, 8.75 , 9.16666667, 9.58333333, 10. ])

>>> numpy.logspace(0, 10, 10, base=numpy.e)

array([ 1.00000000e+00, 3.03773178e+00, 9.22781435e+00,

2.80316249e+01, 8.51525577e+01, 2.58670631e+02,

7.85771994e+02, 2.38696456e+03, 7.25095809e+03,

2.20264658e+04])

Numpy – Creating arrays

# a diagonal matrix

>>> numpy.diag([1,2,3])

array([[1, 0, 0],

[0, 2, 0],

[0, 0, 3]])

>>> b = numpy.zeros(5)

>>> print(b)

[ 0. 0. 0. 0. 0.]

>>> b.dtype

dtype(‘float64’)

>>> n = 1000

>>> my_int_array = numpy.zeros(n, dtype=numpy.int)

>>> my_int_array.dtype

dtype(‘int32’)

>>> c = numpy.ones((3,3))

>>> c

array([[ 1., 1., 1.],

[ 1., 1., 1.],

[ 1., 1., 1.]])

Numpy – Creating arrays

>>> d = numpy.arange(5) # just like range()

>>> print(d)

[0 1 2 3 4]

>>> d[1] = 9.7

>>> print(d) # arrays keep their type even if elements changed

[0 9 2 3 4]

>>> print(d*0.4) # operations create a new array, with new type

[ 0. 3.6 0.8 1.2 1.6]

>>> d = numpy.arange(5, dtype=numpy.float)

>>> print(d)

[ 0. 1. 2. 3. 4.]

>>> numpy.arange(3, 7, 0.5) # arbitrary start, stop and step

array([ 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. , 6.5])

Numpy – array creation and use

>>> x, y = numpy.mgrid[0:5, 0:5] # similar to meshgrid in MATLAB

>>> x

array([[0, 0, 0, 0, 0],

[1, 1, 1, 1, 1],

[2, 2, 2, 2, 2],

[3, 3, 3, 3, 3],

[4, 4, 4, 4, 4]])

# random data

>>> numpy.random.rand(5,5)

array([[ 0.51531133, 0.74085206, 0.99570623, 0.97064334, 0.5819413 ],

[ 0.2105685 , 0.86289893, 0.13404438, 0.77967281, 0.78480563],

[ 0.62687607, 0.51112285, 0.18374991, 0.2582663 , 0.58475672],

[ 0.72768256, 0.08885194, 0.69519174, 0.16049876, 0.34557215],

[ 0.93724333, 0.17407127, 0.1237831 , 0.96840203, 0.52790012]])

Numpy – array creation and use

• File I/O>>> os.system('head DeBilt.txt')

"Stn", "Datum", "Tg", "qTg", "Tn", "qTn", "Tx", "qTx"

001, 19010101, -49, 00, -68, 00, -22, 40

001, 19010102, -21, 00, -36, 30, -13, 30

001, 19010103, -28, 00, -79, 30, -5, 20

001, 19010104, -64, 00, -91, 20, -10, 00

001, 19010105, -59, 00, -84, 30, -18, 00

001, 19010106, -99, 00, -115, 30, -78, 30

001, 19010107, -91, 00, -122, 00, -66, 00

001, 19010108, -49, 00, -94, 00, -6, 00

001, 19010109, 11, 00, -27, 40, 42, 00

0

>>> data = numpy.genfromtxt('DeBilt.txt‘, delimiter=',‘, skip_header=1)

>>> data.shape

(25568, 8)

Numpy – Creating arrays

• File I/O>>> os.system('head DeBilt.txt')

"Stn", "Datum", "Tg", "qTg", "Tn", "qTn", "Tx", "qTx"

001, 19010101, -49, 00, -68, 00, -22, 40

001, 19010102, -21, 00, -36, 30, -13, 30

001, 19010103, -28, 00, -79, 30, -5, 20

001, 19010104, -64, 00, -91, 20, -10, 00

001, 19010105, -59, 00, -84, 30, -18, 00

001, 19010106, -99, 00, -115, 30, -78, 30

001, 19010107, -91, 00, -122, 00, -66, 00

001, 19010108, -49, 00, -94, 00, -6, 00

001, 19010109, 11, 00, -27, 40, 42, 00

0

>>> data = numpy.genfromtxt('DeBilt.txt‘, delimiter=',‘, skip_header=1)

>>> data.shape

(25568, 8)

>>> numpy.savetxt('datasaved.txt', data)

>>> os.system('head datasaved.txt')

1.000000000000000000e+00 1.901010100000000000e+07 -4.900000000000000000e+01

0.000000000000000000e+00 -6.800000000000000000e+01 0.000000000000000000e+00

-2.200000000000000000e+01 4.000000000000000000e+01

1.000000000000000000e+00 1.901010200000000000e+07 -2.100000000000000000e+01

0.000000000000000000e+00 -3.600000000000000000e+01 3.000000000000000000e+01

-1.300000000000000000e+01 3.000000000000000000e+01

1.000000000000000000e+00 1.901010300000000000e+07 -2.800000000000000000e+01

0.000000000000000000e+00 -7.900000000000000000e+01 3.000000000000000000e+01

-5.000000000000000000e+00 2.000000000000000000e+01

Numpy – Creating arrays

>>> M = numpy.random.rand(3,3)

>>> M

array([[ 0.84188778, 0.70928643, 0.87321035],

[ 0.81885553, 0.92208501, 0.873464 ],

[ 0.27111984, 0.82213106, 0.55987325]])

>>>

>>> numpy.save('saved-matrix.npy', M)

>>> numpy.load('saved-matrix.npy')

array([[ 0.84188778, 0.70928643, 0.87321035],

[ 0.81885553, 0.92208501, 0.873464 ],

[ 0.27111984, 0.82213106, 0.55987325]])

>>>

>>> os.system('head saved-matrix.npy')

NUMPYF{'descr': '<f8', 'fortran_order': False, 'shape': (3, 3), }

Ï<

£¾ðê?-sy²æ?$÷ÒVñë?Ù4ê?%dn¸í?Ã[Äjóë?Ä,ZÑ?Ç

ÎåNê?ó7L{êá?0

>>>

Numpy – Creating arrays

• NumPy's main object is the homogeneous multidimensional array called ndarray. – This is a table of elements (usually numbers), all of the same type, indexed by a tuple of

positive integers. Typical examples of multidimensional arrays include vectors, matrices, images and spreadsheets.

– Dimensions usually called axes, number of axes is the rank

[7, 5, -1] An array of rank 1 i.e. It has 1 axis of length 3

[ [ 1.5, 0.2, -3.7] , An array of rank 2 i.e. It has 2 axes, the first [ 0.1, 1.7, 2.9] ] length 3, the second of length 3 (a matrix

with 2 rows and 3 columns

Numpy - ndarray

• ndarray.ndim– the number of axes (dimensions) of the array i.e. the rank.

• ndarray.shape– the dimensions of the array. This is a tuple of integers indicating the size of the array in each

dimension. For a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple is therefore the rank, or number of dimensions, ndim.

• ndarray.size– the total number of elements of the array, equal to the product of the elements of shape.

• ndarray.dtype– an object describing the type of the elements in the array. One can create or specify dtype's using

standard Python types. NumPy provides many, for example bool_, character, int_, int8, int16, int32, int64, float_, float8, float16, float32, float64, complex_, complex64, object_.

• ndarray.itemsize– the size in bytes of each element of the array. E.g. for elements of type float64, itemsize is 8 (=64/8),

while complex32 has itemsize 4 (=32/8) (equivalent to ndarray.dtype.itemsize).

• ndarray.data– the buffer containing the actual elements of the array. Normally, we won't need to use this attribute

because we will access the elements in an array using indexing facilities.

Numpy – ndarray attributes

>>> x = np.array([1,2,3,4])

>>> y = x

>>> x is y

True

>>> id(x), id(y)

(139814289111920, 139814289111920)

>>> x[0] = 9

>>> y

array([9, 2, 3, 4])

>>> x[0] = 1

>>> z = x[:]

>>> x is z

False

>>> id(x), id(z)

(139814289111920, 139814289112080)

>>> x[0] = 8

>>> z

array([8, 2, 3, 4])

Two ndarrays are mutable and may be views to the same memory:

>>> x = np.array([1,2,3,4])

>>> y = x.copy()

>>> x is y

False

>>> id(x), id(y)

(139814289111920, 139814289111840)

>>> x[0] = 9

>>> x

array([9, 2, 3, 4])

>>> y

array([1, 2, 3, 4])

Numpy – array creation and use

>>> a = numpy.arange(4.0)

>>> b = a * 23.4

>>> c = b/(a+1)

>>> c += 10

>>> print c

[ 10. 21.7 25.6 27.55]

>>> arr = numpy.arange(100, 200)

>>> select = [5, 25, 50, 75, -5]

>>> print(arr[select]) # can use integer lists as indices

[105, 125, 150, 175, 195]

>>> arr = numpy.arange(10, 20 )

>>> div_by_3 = arr%3 == 0 # comparison produces boolean array

>>> print(div_by_3)

[ False False True False False True False False True False]

>>> print(arr[div_by_3]) # can use boolean lists as indices

[12 15 18]

>>> arr = numpy.arange(10, 20) . reshape((2,5))

[[10 11 12 13 14]

[15 16 17 18 19]]

Numpy – Creating arrays

>>> arr.sum()

145

>>> arr.mean()

14.5

>>> arr.std()

2.8722813232690143

>>> arr.max()

19

>>> arr.min()

10

>>> div_by_3.all()

False

>>> div_by_3.any()

True

>>> div_by_3.sum()

3

>>> div_by_3.nonzero()

(array([2, 5, 8]),)

Numpy – array methods

>>> arr = numpy.array([4.5, 2.3, 6.7, 1.2, 1.8, 5.5])

>>> arr.sort() # acts on array itself

>>> print(arr)

[ 1.2 1.8 2.3 4.5 5.5 6.7]

>>> x = numpy.array([4.5, 2.3, 6.7, 1.2, 1.8, 5.5])

>>> numpy.sort(x)

array([ 1.2, 1.8, 2.3, 4.5, 5.5, 6.7])

>>> print(x)

[ 4.5 2.3 6.7 1.2 1.8 5.5]

>>> s = x.argsort()

>>> s

array([3, 4, 1, 0, 5, 2])

>>> x[s]

array([ 1.2, 1.8, 2.3, 4.5, 5.5, 6.7])

>>> y[s]

array([ 6.2, 7.8, 2.3, 1.5, 8.5, 4.7])

Numpy – array methods - sorting

• Most array methods have equivalent functions

• Ufuncs provide many element-by-element math, trig., etc. operations– e.g., add(x1, x2), absolute(x), log10(x), sin(x), logical_and(x1, x2)

• See http://numpy.scipy.org

>>> arr.sum() 45 >>> numpy.sum(arr) 45

Numpy – array functions

>>> a = array([[1.0, 2.0], [4.0, 3.0]]) >>> print a [[ 1. 2.] [ 3. 4.]]

>>> a.transpose() array([[ 1., 3.], [ 2., 4.]])

>>> inv(a) array([[-2. , 1. ], [ 1.5, -0.5]])

>>> u = eye(2) # unit 2x2 matrix; "eye" represents "I"

>>> u array([[ 1., 0.], [ 0., 1.]])

>>> j = array([[0.0, -1.0], [1.0, 0.0]])

>>> dot (j, j) # matrix product array([[-1., 0.], [ 0., -1.]])

Numpy – array operations

>>> a = np.array([1, 4, 3, 8, 9, 2, 3], float) >>> np.median(a) 3.0

>>> a = np.array([[1, 2, 1, 3], [5, 3, 1, 8]], float) >>> c = np.corrcoef(a) >>> c array([[ 1. , 0.72870505], [ 0.72870505, 1. ]])

>>> np.cov(a) array([[ 0.91666667, 2.08333333], [ 2.08333333, 8.91666667]])

In addition to the mean, var, and std functions, NumPy supplies several other methods for returning statistical features of arrays. The median can be found:

The correlation coefficient for multiple variables observed at multiple instances can be found for arrays of the form [[x1, x2, …], [y1, y2, …], [z1, z2, …], …] where x, y, z are different observables and the numbers indicate the observation times:

Here the return array c[i,j] gives the correlation coefficient for the ith and jth observables. Similarly, the covariance for data can be found::

Numpy – statistics

• Array operations are implemented in C or Fortran

• Optimised algorithms - i.e. fast!

• Python loops (i.e. for i in a:…) are much slower

• Prefer array operations over loops, especially when speed

important

• Also produces shorter code, often more readable

Using arrays wisely

>>> import numpy

>>> m = numpy.mat([[1,2],[3,4]])

or

>>> a = numpy.array([[1,2],[3,4]])

>>> m = numpy.mat(a)

or

>>> a = numpy.array([[1,2],[3,4]])

>>> m = numpy.asmatrix(a)

For two dimensional arrays NumPy defined a special matrix class in module matrix. Objects are created either with matrix() or mat() or converted from an array with method asmatrix().

Note that the statement m = mat(a) creates a copy of array 'a'. Changing values in 'a' will not affect 'm'. On the other hand, method m = asmatrix(a) returns a new reference to the same data. Changing values in 'a' will affect matrix 'm'.

Numpy – arrays, matrices

>>> a = array([[1,2],[3,4]])

>>> m = mat(a) # convert 2-d array to matrix

>>> m = matrix([[1, 2], [3, 4]])

>>> a[0] # result is 1-dimensional

array([1, 2])

>>> m[0] # result is 2-dimensional

matrix([[1, 2]])

>>> a*a # element-by-element multiplication

array([[ 1, 4], [ 9, 16]])

>>> m*m # (algebraic) matrix multiplication

matrix([[ 7, 10], [15, 22]])

>>> a**3 # element-wise power

array([[ 1, 8], [27, 64]])

>>> m**3 # matrix multiplication m*m*m

matrix([[ 37, 54], [ 81, 118]])

>>> m.T # transpose of the matrix

matrix([[1, 3], [2, 4]])

>>> m.H # conjugate transpose (differs from .T for complex matrices)

matrix([[1, 3], [2, 4]])

>>> m.I # inverse matrix

matrix([[-2. , 1. ], [ 1.5, -0.5]])

Array and matrix operations may be quite different!

Numpy – matrices

• Operator *, dot(), and multiply(): • For array, '*' means element-wise multiplication, and the dot() function is used for matrix

multiplication.• For matrix, '*'means matrix multiplication, and the multiply() function is used for

element-wise multiplication. • Handling of vectors (rank-1 arrays)

• For array, the vector shapes 1xN, Nx1, and N are all different things. Operations like A[:,1] return a rank-1 array of shape N, not a rank-2 of shape Nx1. Transpose on a rank-1 array does nothing.

• For matrix, rank-1 arrays are always upgraded to 1xN or Nx1 matrices (row or column vectors). A[:,1] returns a rank-2 matrix of shape Nx1.

• Handling of higher-rank arrays (rank > 2) • array objects can have rank > 2.• matrix objects always have exactly rank 2.

• Convenience attributes • array has a .T attribute, which returns the transpose of the data.• matrix also has .H, .I, and .A attributes, which return the conjugate transpose, inverse, and

asarray() of the matrix, respectively. • Convenience constructor

• The array constructor takes (nested) Python sequences as initializers. As inarray([[1,2,3],[4,5,6]]).

• The matrix constructor additionally takes a convenient string initializer. As inmatrix("[1 2 3; 4 5 6]")

Numpy – matrices

>>> a = np.array([1,2,3], float) >>> b = np.array([5,2,6], float) >>> a + b array([6., 4., 9.]) >>> a – b array([-4., 0., -3.]) >>> a * b array([5., 4., 18.]) >>> b / a array([5., 1., 2.]) >>> a % b array([1., 0., 3.]) >>> b**a array([5., 4., 216.])

>>> a = np.array([[1, 2], [3, 4], [5, 6]], float) >>> b = np.array([-1, 3], float) >>> a array([[ 1., 2.], [ 3., 4.], [ 5., 6.]]) >>> b array([-1., 3.]) >>> a + b array([[ 0., 5.], [ 2., 7.], [ 4., 9.]])

Numpy – array mathematics

>>> a = np.array([1,2,3], float) >>> b = np.array([5,2,6], float) >>> a + b array([6., 4., 9.]) >>> a – b array([-4., 0., -3.]) >>> a * b array([5., 4., 18.]) >>> b / a array([5., 1., 2.]) >>> a % b array([1., 0., 3.]) >>> b**a array([5., 4., 216.])

>>> a = np.array([[1, 2], [3, 4], [5, 6]], float) >>> b = np.array([-1, 3], float) >>> a array([[ 1., 2.], [ 3., 4.], [ 5., 6.]]) >>> b array([-1., 3.]) >>> a + b array([[ 0., 5.], [ 2., 7.], [ 4., 9.]])

>>> a = np.array([[1, 2], [3, 4], [5, 6]], float) >>> b = np.array([-1, 3], float)

>>> a * aarray([[ 1., 4.], [ 9., 16.], [ 25., 36.]])>>> b * barray([ 1., 9.])>>> a * barray([[ -1., 6.], [ -3., 12.], [ -5., 18.]])>>>

Numpy – array mathematics

>>> A = np.array([[n+m*10 for n in range(5)] for m in range(5)])>>> v1 = arange(0, 5)>>> Aarray([[ 0, 1, 2, 3, 4],[10, 11, 12, 13, 14],[20, 21, 22, 23, 24],[30, 31, 32, 33, 34],[40, 41, 42, 43, 44]])>>> v1array([0, 1, 2, 3, 4])>>> np.dot(A,A)array([[ 300, 310, 320, 330, 340], [1300, 1360, 1420, 1480, 1540], [2300, 2410, 2520, 2630, 2740], [3300, 3460, 3620, 3780, 3940], [4300, 4510, 4720, 4930, 5140]])>>>>>> np.dot(A,v1)array([ 30, 130, 230, 330, 430])>>> np.dot(v1,v1)30>>>

Numpy – array mathematics

>>> A = np.array([[n+m*10 for n in range(5)] for m in range(5)])>>> v1 = arange(0, 5)>>> Aarray([[ 0, 1, 2, 3, 4],[10, 11, 12, 13, 14],[20, 21, 22, 23, 24],[30, 31, 32, 33, 34],[40, 41, 42, 43, 44]])>>> v1array([0, 1, 2, 3, 4])>>> np.dot(A,A)array([[ 300, 310, 320, 330, 340], [1300, 1360, 1420, 1480, 1540], [2300, 2410, 2520, 2630, 2740], [3300, 3460, 3620, 3780, 3940], [4300, 4510, 4720, 4930, 5140]])>>>>>> np.dot(A,v1)array([ 30, 130, 230, 330, 430])>>> np.dot(v1,v1)30>>>

Alternatively, we can cast the array objects to the type matrix. This changes the behavior of the standard arithmetic operators +, -, * to use matrix algebra.>>> M = np.matrix(A)>>> v = np.matrix(v1).T>>> vmatrix([[0], [1], [2], [3], [4]])>>> M*vmatrix([[ 30], [130], [230], [330], [430]])>>> v.T * v # inner productmatrix([[30]])# standard matrix algebra applies>>> v + M*vmatrix([[ 30], [131], [232], [333], [434]])

Numpy – array mathematics

• User friendly, but powerful, plotting capabilites for python• http://matplotlib.sourceforge.net/

• Once installed >>> import pylab

• Settings can be customised by editing ~/.matplotlib/matplotlibrc– default font, colours, layout, etc.

• Helpful website– many examples

Plotting - matplotlib

Pylab is a module in matplotlib that gets installed alongside matplotlib; and matplotlib.pyplot is a module in matplotlib.• Pyplot provides the state-machine interface to the underlying plotting library in

matplotlib. This means that figures and axes are implicitly and automatically created to achieve the desired plot. Setting a title will then automatically set that title to the current axes object.

• Pylab combines the pyplot functionality (for plotting) with the numpy functionality (for mathematics and for working with arrays) in a single namespace, For example, one can call the sin and cos functions just like you could in MATLAB, as well as having all the features of pyplot.

• The pyplot interface is generally preferred for non-interactive plotting (i.e., scripting). The pylab interface is convenient for interactive calculations and plotting, as it minimizes typing. Note that this is what you get if you use the ipython shell with the --pylab option, which imports everything from pylab and makes plotting fully interactive.

Pyplot and pylab

$ python

Python 2.7.3 (default, Aug 9 2012, 17:23:57)

[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import matplotlib.pyplot as plt

>>> plt.plot([1,2,3,4])

[<matplotlib.lines.Line2D object at 0x1fe53d0>]

>>> plt.ylabel('some numbers')

<matplotlib.text.Texy object at 0x1d6ad90>

>>> plt.show()

$ ipython –-pylab

Python 2.7.3 (default, Aug 9 2012, 17:23:57)

Type "copyright", "credits" or "license" for more information.

IPython 0.13.2 -- An enhanced Interactive Python.

? -> Introduction and overview of IPython's features.

%quickref -> Quick reference.

help -> Python's own help system.

object? -> Details about 'object', use 'object??' for extra details.

Welcome to pylab, a matplotlib-based Python environment [backen: GTKAgg].

For more information, type ‘help(pylab)’

In [1]: plot([1,2,3,4])

Pyplot and pylab

Function Descriptionacorr plot the autocorrelation functionannotate annotate something in the figurearrow add an arrow to the axesaxes create a new axesaxhline draw a horizontal line across axesaxvline draw a vertical line across axesaxhspan draw a horizontal bar across axesaxvspan draw a vertical bar across axesaxis set or return the current axis limitsbarbs a (wind) barb plotbar make a bar chartbarh a horizontal bar chartbroken_barh a set of horizontal bars with gapsbox set the axes frame on/off stateboxplot make a box and whisker plotcla clear current axesclabel label a contour plotclf clear a figure windowclim adjust the color limits of the current imageclose close a figure windowcolorbar add a colorbar to the current figurecohere make a plot of coherencecontour make a contour plotcontourf make a filled contour plotcsd make a plot of cross spectral densitydelaxes delete an axes from the current figuredraw Force a redraw of the current figureerrorbar make an errorbar graph

figlegend make legend on the figure rather than the axes

figimage make a figure imagefigtext add text in figure coordsfigure create or change active figurefill make filled polygonsfill_between make filled polygons between two curves

Function Descriptiongca return the current axesgcf return the current figuregci get the current image, or Nonegetp get a graphics propertygrid set whether gridding is onhexbin make a 2D hexagonal binning plothist make a histogramhold set the axes hold stateioff turn interaction mode offion turn interaction mode onisinteractive return True if interaction mode is onimread load image file into arrayimsave save array as an image fileimshow plot image dataishold return the hold state of the current axeslegend make an axes legend

locator_params adjust parameters used in locating axis ticks

loglog a log log plot

matshow display a matrix in a new figure preserving aspect

margins set margins used in autoscalingpcolor make a pseudocolor plot

pcolormesh make a pseudocolor plot using a quadrilateral mesh

pie make a pie chartplot make a line plotplot_date plot dates

plotfile plot column data from an ASCII tab/space/comma delimited file

pie pie chartspolar make a polar plot on a PolarAxespsd make a plot of power spectral densityquiver make a direction field (arrows) plotrc control the default params

Function Descriptiongca return the current axesgcf return the current figuregci get the current image, or Nonegetp get a graphics propertygrid set whether gridding is onhexbin make a 2D hexagonal binning plothist make a histogramhold set the axes hold stateioff turn interaction mode offion turn interaction mode onisinteractive return True if interaction mode is onimread load image file into arrayimsave save array as an image fileimshow plot image dataishold return the hold state of the current axeslegend make an axes legend

locator_params adjust parameters used in locating axis ticks

loglog a log log plot

matshow display a matrix in a new figure preserving aspect

margins set margins used in autoscalingpcolor make a pseudocolor plot

pcolormesh make a pseudocolor plot using a quadrilateral mesh

pie make a pie chartplot make a line plotplot_date plot dates

plotfile plot column data from an ASCII tab/space/comma delimited file

pie pie chartspolar make a polar plot on a PolarAxespsd make a plot of power spectral densityquiver make a direction field (arrows) plotrc control the default params

Matplotlib.pyplot example

>>> import matplotlib.pylab as plt

>>> x = plt.linspace(0, 5, 10)

>>> y = x ** 2

>>> figure()

>>> plot(x, y, ’r’)

>>> xlabel(’x’)

>>> ylabel(’y’)

>>> title(’title’)

>>> show()

The easiest way to get started with plotting using matplotlib is often to use the MATLAB-like API provided by matplotlib.

Matplotlib.pyplot example

>>> plt.subplot(1,2,1)

>>> plt.plot(x, y, ’r--’)

>>> plt.subplot(1,2,2)

>>> plt.plot(y, x, ’g*-’);

Most of the plotting related functions in MATLAB are covered by the pylab module. For example, subplot and color/symbol selection

Matplotlib.pyplot example

import numpy as np import matplotlib.pyplot as plt

def f(t): return np.exp(-t) * np.cos(2*np.pi*t)

t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02)

plt.figure(1) plt.subplot(211) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')

plt.subplot(212) plt.plot(t2, np.cos(2*np.pi*t2), 'r--')

Working with multiple figures and axes. The subplot() command specifies numrows, numcols, fignum, where fignum ranges from 1 to numrows*numcols.

Matplotlib.pyplot example

fig = plt.figure()

axes1 = fig.add_axes([0.1, 0.1,

0.8, 0.8]) # main axes

axes2 = fig.add_axes([0.2, 0.5,

0.4, 0.3]) # inset axes

# main figure

axes1.plot(x, y, ’r’)

axes1.set_xlabel(’x’)

axes1.set_ylabel(’y’)

axes1.set_title(’title’)

# insert

axes2.plot(y, x, ’g’)

axes2.set_xlabel(’y’)

axes2.set_ylabel(’x’)

axes2.set_title(’insert title’);

Matplotlib can be used in object oriented approach which is particularly useful when we deal with subplots

Matplotlib.pyplot example

fig, axes = plt.subplots(nrows=1,

ncols=2)

for ax in axes:

ax.plot(x, y, ’r’)

ax.set_xlabel(’x’)

ax.set_ylabel(’y’)

ax.set_title(’title’)

fig.tight_layout()

With multiple plots on one screen sometimes the labels are getting in the way. Solve this with tight_layout

Matplotlib.pyplot example

>>> ax.set_title("title");

>>> ax.set_xlabel("x")

>>> ax.set_ylabel("y");

>>> ax.legend(["curve1", "curve2", "curve3"]);

>>> ax.plot(x, x**2, label="curve1")

>>> ax.plot(x, x**3, label="curve2")

>>> ax.legend()

>>> ax.legend(loc=0) # let matplotlib decide

>>> ax.legend(loc=1) # upper right corner

>>> ax.legend(loc=2) # upper left corner

>>> ax.legend(loc=3) # lower left corner

>>> ax.legend(loc=4) # lower right corner

Labels and legends and titlesThese figure decrations are essential for presenting your data to others (in papers and in oral presentations).

Matplotlib.pyplot example

>>> ax.set_title("title");

>>> ax.set_xlabel("x")

>>> ax.set_ylabel("y");

>>> ax.legend(["curve1", "curve2", "curve3"]);

>>> ax.plot(x, x**2, label="curve1")

>>> ax.plot(x, x**3, label="curve2")

>>> ax.legend()

>>> ax.legend(loc=0) # let matplotlib decide

>>> ax.legend(loc=1) # upper right corner

>>> ax.legend(loc=2) # upper left corner

>>> ax.legend(loc=3) # lower left corner

>>> ax.legend(loc=4) # lower right corner

Labels and legends and titlesThese figure decrations are essential for presenting your data to others (in papers and in oral presentations).>>> fig, ax = plt.subplots()

>>> ax.plot(x, x**2, label="y = x**2")

>>> ax.plot(x, x**3, label="y = x**3")

>>> ax.legend(loc=2); # upper left corner

>>> ax.set_xlabel(’x’)

>>> ax.set_ylabel(’y’)

>>> ax.set_title(’title’);

Matplotlib.pyplot example

>>> fig, ax = plt.subplots()

>>> ax.plot(x, x**2, label=r"$y = \alpha^2$")

>>> ax.plot(x, x**3, label=r"$y = \alpha^3$")

>>> ax.legend(loc=2) # upper left corner

>>> ax.set_xlabel(r’$\alpha$’, fontsize=18)

>>> ax.set_ylabel(r’$y$’, fontsize=18)

>>> ax.set_title(’title’);

Changing the font size and family and using LaTeX formatted tekst.

Matplotlib.pyplot example

>>> fig, ax = plt.subplots()

>>> ax.plot(x, x**2, label=r"$y = \alpha^2$")

>>> ax.plot(x, x**3, label=r"$y = \alpha^3$")

>>> ax.legend(loc=2) # upper left corner

>>> ax.set_xlabel(r’$\alpha$’, fontsize=18)

>>> ax.set_ylabel(r’$y$’, fontsize=18)

>>> ax.set_title(’title’);

Changing the font size and family and using LaTeX formatted tekst.

matplotlib.rcParams.update({’font.size’: 18, ’font.family’: ’serif’})

Matplotlib.pyplot example

fig, ax = plt.subplots()

ax.plot(x, x+1, color="red", alpha=0.5) # half-transparant red

ax.plot(x, x+2, color="#1155dd") # RGB hex code for a bluish color

ax.plot(x, x+3, color="#15cc55") # RGB hex code for a greenish color

You have full control over colors, linewidths and linetypes.

For colors one can use simple syntax like ‘b’ for blue, ‘g’ for green, etc. or RGB hex codes with transparency alpha code:

Matplotlib.pyplot example

fig, ax = plt.subplots(figsize=(12,6))

ax.plot(x, x+1, color="blue", linewidth=0.25)

ax.plot(x, x+2, color="blue", linewidth=0.50)

ax.plot(x, x+3, color="blue", linewidth=1.00)

ax.plot(x, x+4, color="blue", linewidth=2.00)

# possible linestype options ‘-‘, ‘{’, ‘-.’, ‘:’, ‘steps’

ax.plot(x, x+5, color="red", lw=2, linestyle=’-’)

ax.plot(x, x+6, color="red", lw=2, ls=’-.’)

ax.plot(x, x+7, color="red", lw=2, ls=’:’)

# custom dash

line, = ax.plot(x, x+8, color="black", lw=1.50)

line.set_dashes([5, 10, 15, 10]) # format: line length, space length, ...

# possible marker symbols: marker = ’+’, ’o’, ’*’, ’s’, ’,’, ’.’, ’1’, ’2’, ’3’, ’4’, ...

ax.plot(x, x+ 9, color="green", lw=2, ls=’*’, marker=’+’)

ax.plot(x, x+10, color="green", lw=2, ls=’*’, marker=’o’)

ax.plot(x, x+11, color="green", lw=2, ls=’*’, marker=’s’)

ax.plot(x, x+12, color="green", lw=2, ls=’*’, marker=’1’)

# marker size and color

ax.plot(x, x+13, color="purple", lw=1, ls=’-’, marker=’o’, markersize=2)

ax.plot(x, x+14, color="purple", lw=1, ls=’-’, marker=’o’, markersize=4)

ax.plot(x, x+15, color="purple", lw=1, ls=’-’, marker=’o’, markersize=8, markerfacecolor="red")

ax.plot(x, x+16, color="purple", lw=1, ls=’-’, marker=’s’, markersize=8,

markerfacecolor="yellow", markeredgewidth=2, markeredgecolor="blue");

Line and marker styles are coded:

Matplotlib.pyplot example

fig, ax = plt.subplots(figsize=(12,6))

ax.plot(x, x+1, color="blue", linewidth=0.25)

ax.plot(x, x+2, color="blue", linewidth=0.50)

ax.plot(x, x+3, color="blue", linewidth=1.00)

ax.plot(x, x+4, color="blue", linewidth=2.00)

# possible linestype options ‘-‘, ‘{’, ‘-.’, ‘:’, ‘steps’

ax.plot(x, x+5, color="red", lw=2, linestyle=’-’)

ax.plot(x, x+6, color="red", lw=2, ls=’-.’)

ax.plot(x, x+7, color="red", lw=2, ls=’:’)

# custom dash

line, = ax.plot(x, x+8, color="black", lw=1.50)

line.set_dashes([5, 10, 15, 10]) # format: line length, space length, ...

# possible marker symbols: marker = ’+’, ’o’, ’*’, ’s’, ’,’, ’.’, ’1’, ’2’, ’3’, ’4’, ...

ax.plot(x, x+ 9, color="green", lw=2, ls=’*’, marker=’+’)

ax.plot(x, x+10, color="green", lw=2, ls=’*’, marker=’o’)

ax.plot(x, x+11, color="green", lw=2, ls=’*’, marker=’s’)

ax.plot(x, x+12, color="green", lw=2, ls=’*’, marker=’1’)

# marker size and color

ax.plot(x, x+13, color="purple", lw=1, ls=’-’, marker=’o’, markersize=2)

ax.plot(x, x+14, color="purple", lw=1, ls=’-’, marker=’o’, markersize=4)

ax.plot(x, x+15, color="purple", lw=1, ls=’-’, marker=’o’, markersize=8, markerfacecolor="red")

ax.plot(x, x+16, color="purple", lw=1, ls=’-’, marker=’s’, markersize=8,

markerfacecolor="yellow", markeredgewidth=2, markeredgecolor="blue");

Line and marker styles are coded:

Matplotlib.pyplot example

>>> fig, ax1 = plt.subplots()

>>> ax1.plot(x, x**2, lw=2, color="blue")

>>> ax1.set_ylabel(r"area $(m^2)$", fontsize=18, color="blue")

>>> for label in ax1.get_yticklabels():

... label.set_color("blue")

...

>>> ax2 = ax1.twinx()

>>> ax2.plot(x, x**3, lw=2, color="red")

>>> ax2.set_ylabel(r"volume $(m^3)$", fontsize=18, color="red")

>>> for label in ax2.get_yticklabels():

... label.set_color("red")

Sometimes it is useful to have dual x or y axes in a figure; for example, when plotting curves with different

units together. Matplotlib supports this with the twinx and twiny functions:

Matplotlib.pyplot example

>>> fig, ax1 = plt.subplots()

>>> ax1.plot(x, x**2, lw=2, color="blue")

>>> ax1.set_ylabel(r"area $(m^2)$", fontsize=18, color="blue")

>>> for label in ax1.get_yticklabels():

... label.set_color("blue")

...

>>> ax2 = ax1.twinx()

>>> ax2.plot(x, x**3, lw=2, color="red")

>>> ax2.set_ylabel(r"volume $(m^3)$", fontsize=18, color="red")

>>> for label in ax2.get_yticklabels():

... label.set_color("red")

Sometimes it is useful to have dual x or y axes in a figure; for example, when plotting curves with different

units together. Matplotlib supports this with the twinx and twiny functions:

Matplotlib.pyplot example

from matplotlib.pyplot import figure, showfrom matplotlib.patches import Ellipseimport numpy as np

if 1: fig = figure(1,figsize=(8,5)) ax = fig.add_subplot(111, autoscale_on=False, xlim=(-1,5), ylim=(-4,3))

t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = ax.plot(t, s, lw=3, color='purple')

ax.annotate('arrowstyle', xy=(0, 1), xycoords='data', xytext=(-50, 30), textcoords='offset points', arrowprops=dict(arrowstyle="->") )

ax.annotate('arc3', xy=(0.5, -1), xycoords='data', xytext=(-30, -30), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2") )

ax.annotate('arc', xy=(1., 1), xycoords='data', xytext=(-40, 30), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc,angleA=0,armA=30,rad=10"), )

ax.annotate('arc', xy=(1.5, -1), xycoords='data', xytext=(-40, -30), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc,angleA=0,armA=20,angleB=-90,armB=15,rad=7"), )

ax.annotate('angle', xy=(2., 1), xycoords='data', xytext=(-50, 30), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="angle,angleA=0,angleB=90,rad=10"), )

ax.annotate('angle3', xy=(2.5, -1), xycoords='data', xytext=(-50, -30), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="angle3,angleA=0,angleB=-90"), )

ax.annotate('angle', xy=(3., 1), xycoords='data', xytext=(-50, 30), textcoords='offset points', bbox=dict(boxstyle="round", fc="0.8"), arrowprops=dict(arrowstyle="->", connectionstyle="angle,angleA=0,angleB=90,rad=10"), )

ax.annotate('angle', xy=(3.5, -1), xycoords='data', xytext=(-70, -60), textcoords='offset points', size=20, bbox=dict(boxstyle="round4,pad=.5", fc="0.8"), arrowprops=dict(arrowstyle="->", connectionstyle="angle,angleA=0,angleB=-90,rad=10"), )

ax.annotate('angle', xy=(4., 1), xycoords='data', xytext=(-50, 30), textcoords='offset points', bbox=dict(boxstyle="round", fc="0.8"), arrowprops=dict(arrowstyle="->", shrinkA=0, shrinkB=10, connectionstyle="angle,angleA=0,angleB=90,rad=10"), )

ann = ax.annotate('', xy=(4., 1.), xycoords='data', xytext=(4.5, -1), textcoords='data', arrowprops=dict(arrowstyle="<->", connectionstyle="bar", ec="k", shrinkA=5, shrinkB=5, ) )

if 1: fig = figure(2) fig.clf() ax = fig.add_subplot(111, autoscale_on=False, xlim=(-1,5), ylim=(-5,3))

el = Ellipse((2, -1), 0.5, 0.5) ax.add_patch(el)

ax.annotate('$->$', xy=(2., -1), xycoords='data', xytext=(-150, -140), textcoords='offset points', bbox=dict(boxstyle="round", fc="0.8"), arrowprops=dict(arrowstyle="->", patchB=el, connectionstyle="angle,angleA=90,angleB=0,rad=10"), )

ax.annotate('fancy', xy=(2., -1), xycoords='data', xytext=(-100, 60), textcoords='offset points', size=20, #bbox=dict(boxstyle="round", fc="0.8"), arrowprops=dict(arrowstyle="fancy", fc="0.6", ec="none", patchB=el, connectionstyle="angle3,angleA=0,angleB=-90"), )

ax.annotate('simple', xy=(2., -1), xycoords='data', xytext=(100, 60), textcoords='offset points', size=20, #bbox=dict(boxstyle="round", fc="0.8"), arrowprops=dict(arrowstyle="simple", fc="0.6", ec="none", patchB=el, connectionstyle="arc3,rad=0.3"), )

ax.annotate('wedge', xy=(2., -1), xycoords='data', xytext=(-100, -100), textcoords='offset points', size=20, #bbox=dict(boxstyle="round", fc="0.8"), arrowprops=dict(arrowstyle="wedge,tail_width=0.7", fc="0.6", ec="none", patchB=el, connectionstyle="arc3,rad=-0.3"), )

ann = ax.annotate('wedge', xy=(2., -1), xycoords='data', xytext=(0, -45), textcoords='offset points', size=20, bbox=dict(boxstyle="round", fc=(1.0, 0.7, 0.7), ec=(1., .5, .5)), arrowprops=dict(arrowstyle="wedge,tail_width=1.", fc=(1.0, 0.7, 0.7), ec=(1., .5, .5), patchA=None, patchB=el, relpos=(0.2, 0.8), connectionstyle="arc3,rad=-0.1"), )

ann = ax.annotate('wedge', xy=(2., -1), xycoords='data', xytext=(35, 0), textcoords='offset points', size=20, va="center", bbox=dict(boxstyle="round", fc=(1.0, 0.7, 0.7), ec="none"), arrowprops=dict(arrowstyle="wedge,tail_width=1.", fc=(1.0, 0.7, 0.7), ec="none", patchA=None, patchB=el, relpos=(0.2, 0.5), ) )

show()

Matplotlib.pyplot example

>>> n = array([0,1,2,3,4,5])

>>> fig, axes = plt.subplots(1, 4, figsize=(12,3))

>>> axes[0].scatter(xx, xx + 0.25*randn(len(xx)))

>>> axes[0].set_title("scatter")

>>> axes[1].step(n, n**2, lw=2)

>>> axes[1].set_title("step")

>>> axes[2].bar(n, n**2, align="center", width=0.5, alpha=0.5)

>>> axes[2].set_title("bar")

>>> axes[3].fill_between(x, x**2, x**3, color="green", alpha=0.5)

>>> axes[3].set_title("fill_between")

Other 2D plt styles

Matplotlib.pyplot example

>>> n = array([0,1,2,3,4,5])

>>> fig, axes = plt.subplots(1, 4, figsize=(12,3))

>>> axes[0].scatter(xx, xx + 0.25*randn(len(xx)))

>>> axes[0].set_title("scatter")

>>> axes[1].step(n, n**2, lw=2)

>>> axes[1].set_title("step")

>>> axes[2].bar(n, n**2, align="center", width=0.5, alpha=0.5)

>>> axes[2].set_title("bar")

>>> axes[3].fill_between(x, x**2, x**3, color="green", alpha=0.5)

>>> axes[3].set_title("fill_between")

Other 2D plt styles

Matplotlib.pyplot example

>>> n = np.random.randn(100000)

>>> fig, axes = plt.subplots(1, 2, figsize=(12,4))

>>> axes[0].hist(n)

>>> axes[0].set_title("Default histogram")

>>> axes[0].set_xlim((min(n), max(n)))

>>> axes[1].hist(n, cumulative=True, bins=50)

>>> axes[1].set_title("Cumulative detailed histogram")

>>> axes[1].set_xlim((min(n), max(n)));

Histograms are also a very useful visualisation tool

Matplotlib.pyplot example

>>> n = np.random.randn(100000)

>>> fig, axes = plt.subplots(1, 2, figsize=(12,4))

>>> axes[0].hist(n)

>>> axes[0].set_title("Default histogram")

>>> axes[0].set_xlim((min(n), max(n)))

>>> axes[1].hist(n, cumulative=True, bins=50)

>>> axes[1].set_title("Cumulative detailed histogram")

>>> axes[1].set_xlim((min(n), max(n)));

Histograms are also a very useful visualisation tool

Matplotlib.pyplot example

import numpy as np import matplotlib.pyplot as plt

mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000)

# the histogram of the data n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)

plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True)

Working with text.The text() command can be used to add text in an arbitrary location, and the xlabel(), ylabel() and title() are used to add text in the indicated locations.

Matplotlib.pyplot example

import numpy.numarray as na

from pylab import *

labels = ["Baseline", "System"]data = [3.75 , 4.75]error = [0.3497 , 0.3108]

xlocations = na.array(range(len(data)))+0.5width = 0.5bar(xlocations, data, yerr=error, width=width)yticks(range(0, 8))xticks(xlocations+ width/2, labels)xlim(0, xlocations[-1]+width*2)title("Average Ratings on the Training Set")

show()

Bar chart.

Matplotlib.pyplot example

import numpy as npimport matplotlib.pyplot as plt

fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True)ax = axs[0,0]ax.errorbar(x, y, yerr=yerr, fmt='o')ax.set_title('Vert. symmetric')# With 4 subplots, reduce the number # of axis ticks to avoid crowding.ax.locator_params(nbins=4)ax = axs[0,1]ax.errorbar(x, y, xerr=xerr, fmt='o')ax.set_title('Hor. symmetric')ax = axs[1,0]ax.errorbar(x, y, yerr=[yerr, 2*yerr], xerr=[xerr, 2*xerr], fmt='--o')ax.set_title('H, V asymmetric')ax = axs[1,1]ax.set_yscale('log')# Here we have to be careful to keep all y values positive:ylower = np.maximum(1e-2, y - yerr)yerr_lower = y - ylowerax.errorbar(x, y, yerr=[yerr_lower, 2*yerr], xerr=xerr, fmt='o', ecolor='g')ax.set_title('Mixed sym., log y')fig.suptitle('Variable errorbars')plt.show()

Error bars.

Matplotlib.pyplot example

• Scatter plots

import numpy as npimport pylab as plt

x = np.random.random(50)y = np.random.random(50)c = np.random.random(50) # color of pointss = 500 * np.random.random(50) # size of points

fig, ax = plt.subplots()im = ax.scatter(x, y, c=c, s=s, cmap=plt.cm.jet)

# Add a colorbarfig.colorbar(im, ax=ax)

# set the color limits - not necessary here, but good to know how.im.set_clim(0.0, 1.0)

Matplotlib.pyplot example

>>> import pyfits

>>> import matplotlib.pyplot as plt

>>> data = pyfits.getdata('m33.fits')

>>> plt.imshow(data, cmap='gray')

<matplotlib.image.AxesImage object at 0x7f6c09b1a250>

>>> plt.colorbar()

<matplotlib.colorbar.Colorbar instance at 0x7f6c09aabb90>

>>> plt.show()

Colormaps and contour figures are useful for plotting functions of two variables. In most of these functions we will use a colormap to encode one dimension of the data. There are a number of predefined colormaps.

Matplotlib.pyplot example

>>> import pyfits

>>> import matplotlib.pyplot as plt

>>> data = pyfits.getdata('m33.fits')

>>> plt.imshow(data, cmap='gray')

<matplotlib.image.AxesImage object at 0x7f6c09b1a250>

>>> plt.colorbar()

<matplotlib.colorbar.Colorbar instance at 0x7f6c09aabb90>

>>> plt.show()

Colormaps and contour figures are useful for plotting functions of two variables. In most of these functions we will use a colormap to encode one dimension of the data. There are a number of predefined colormaps.

Matplotlib.pyplot example

>>> import pyfits

>>> import matplotlib.pyplot as plt

>>> data = pyfits.getdata('m33.fits')

>>> plt.imshow(data, cmap='gray')

<matplotlib.image.AxesImage object at 0x7f6c09b1a250>

>>> plt.colorbar()

<matplotlib.colorbar.Colorbar instance at 0x7f6c09aabb90>

>>> plt.show()

Colormaps and contour figures are useful for plotting functions of two variables. In most of these functions we will use a colormap to encode one dimension of the data. There are a number of predefined colormaps.

Matplotlib.pyplot example

>>> import pyfits

>>> import matplotlib.pyplot as plt

>>> data = pyfits.getdata('m33.fits')

>>> plt.imshow(data, cmap='gray')

<matplotlib.image.AxesImage object at 0x7f6c09b1a250>

>>> plt.colorbar()

<matplotlib.colorbar.Colorbar instance at 0x7f6c09aabb90>

>>> plt.show()

Colormaps and contour figures are useful for plotting functions of two variables. In most of these functions we will use a colormap to encode one dimension of the data. There are a number of predefined colormaps.

Matplotlib.pyplot example

from mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as plt fig = plt.figure()ax = fig.add_subplot(111, projection='3d') x =[1,2,3,4,5,6,7,8,9,10]y =[5,6,2,3,13,4,1,2,4,8]z =[2,3,3,3,5,7,9,11,9,10] ax.scatter(x, y, z, c='r', marker='o') ax.set_xlabel('X Label')ax.set_ylabel('Y Label')ax.set_zlabel('Z Label') plt.show()

3D plots.

Matplotlib.pyplot example

from mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as plt fig = plt.figure()ax = fig.add_subplot(111, projection='3d') x =[1,2,3,4,5,6,7,8,9,10]y =[5,6,2,3,13,4,1,2,4,8]z =[2,3,3,3,5,7,9,11,9,10] ax.scatter(x, y, z, c='r', marker='o') ax.set_xlabel('X Label')ax.set_ylabel('Y Label')ax.set_zlabel('Z Label') plt.show()

3D plots.

Matplotlib.pyplot example

from numpy import *import pylab as pimport mpl_toolkits.mplot3d.axes3d as p3

# u and v are parametric variables.# u is an array from 0 to 2*pi, with 100 elementsu=r_[0:2*pi:100j]# v is an array from 0 to 2*pi, with 100 elementsv=r_[0:pi:100j]# x, y, and z are the coordinates of the points for plotting# each is arranged in a 100x100 arrayx=10*outer(cos(u),sin(v))y=10*outer(sin(u),sin(v))z=10*outer(ones(size(u)),cos(v)

fig=p.figure()ax = p3.Axes3D(fig)ax.plot_wireframe(x,y,z)ax.set_xlabel('X')ax.set_ylabel('Y')ax.set_zlabel('Z')p.show()

3D plots.

Matplotlib.pyplot example

Recommended