Numpy and Matplotlib
Numpy and Matplotlib
LECTURE 5:
NUMPY AND
MATPLOTLIB
Introduction to Scientific Python, CME 193
Feb. 6, 2014
Download exercises from:
web.stanford.edu/~ermartin/Teaching/CME193-Winter15
Eileen Martin
Overview
• Numpy: basic objects, methods, functions
• Numpy: linear algebra
• Numpy: random
• Matplotlib: 2D plots
• Matplotlib: 3D plots
• Scipy vs Numpy
• Discuss assignment 4
3
Numpy
• Fundamental package for working with N-dimensional
array objects (vector, matrix, tensor, …)
import numpy as np
x0 = np.array([True,True,False])
x1 = np.array([2,1,4], np.int32)
x2 = np.array([[2,0,4],[3,2,7]])
x3 = np.empty([3,2])
x4 = np.empty_like(x2)
x5 = np.zeros(4, np.complex64)
x6 = np.arange(1,9,2.0)
x7 = np.diag([1, 2, 4])
x8 = np.linspace(0,np.pi,10)
http://docs.scipy.org/doc/numpy/reference/routines.array-creation.html
5
Array broadcasting:
Automatically make copies of arrays to fill in length 1 dimensions
0 0 0 0 1 2 0 1 2
10 10 10 10 11 12
0 0 0 0 1 2 0 1 2
10 10 10 0 1 2 10 11 12
0 1 2 0 1 2
0
10 10 11 12
7
Reshaping an array
• Use reshape to modify the dimensions of an array while
leaving the total number of elements the same
A = np.arange(8)
A.reshape(2,4)
# gives [[0,1,2,3],[4,5,6,7]]
• Use resize to remove elements or append 0’s in place
(size can change under some circumstances*)
A.resize(2,3)
• Use resize to return a copy with removed elements or
repeated copies
b = resize(a,(2,4))
9
Overview
• Numpy: basic objects, methods, functions
• Numpy: linear algebra
• Numpy: random
• Matplotlib: 2D plots
• Matplotlib: 3D plots
• Scipy vs Numpy
• Discuss assignment 4
10
http://docs.scipy.org/doc/numpy/reference/routines.linalg.html
12
Overview
• Numpy: basic objects, methods, functions
• Numpy: linear algebra
• Numpy: random
• Matplotlib: 2D plots
• Matplotlib: 3D plots
• Scipy vs Numpy
• Discuss assignment 4
13
Numpy: Random
• In the linear regression exercise, those ‘measurements’
were actually generated by numpy.random
x = np.random.randn(50) # draw 50 numbers from the standard normal dist.
y = 3.5*x+2+np.random.randn(50)*0.3 # apply a linear transform and add noise
http://docs.scipy.org/doc/numpy/reference/routines.random.html
14
Numpy: Random
• The numpy.random module has many distributions
you can draw from (a very small subset of these is in the table)
name explanation
rand(n0,n1,…) ndarray of random values from uniform [0,1]
randn(n0,n1,…) random standard normal
randint(lo, [hi, size]) random integers [lo, hi)
shuffle(seq) shuffle sequence randomly
choice(seq,[size,replace,p]) sample k items from a 1D array with or
without replacement
chisquare(df,[size]) sample from Chi-squared distribution with df
degrees of freedom
exponential([scale,size]) sample from exponential distribution
http://docs.scipy.org/doc/numpy/reference/routines.random.html
15
Overview
• Numpy: basic objects, methods, functions
• Numpy: linear algebra
• Numpy: random
• Matplotlib: 2D plots
• Matplotlib: 3D plots
• Scipy vs Numpy
• Discuss assignment 4
16
Matplotlib: 2D plots
• Matplotlib is the 2D Python plotting library
• We’ll mostly use matplotlib.pyplot
• There are tons of options, so consult the documentation:
http://matplotlib.org/users/beginner.html
• matplotlib.pyplot can do many types of visualizations including:
• Histograms, bar charts (using hist)
• Error bars on plots, box plots (using boxplot, errorbar)
• Scatterplots (using scatter)
• Line plots (using plot)
• Contour maps (using contour or tricontour)
• Images (matrix to image) (using imshow)
• Stream plots which show derivatives at many locations (streamplot)
• Pie charts, polar charts (using pie, polar)
17
import numpy as np
import matplotlib.pyplot as plt
Overview
• Numpy: basic objects, methods, functions
• Numpy: linear algebra
• Numpy: random
• Matplotlib: 2D plots
• Matplotlib: 3D plots
• Scipy vs Numpy
• Discuss assignment 4
20
Matplotlib: 3D plots
• To do 3D plotting, we’ll use mpl_toolkits.mplot3d
Axes3D class
• Documentation:
http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#mplot3d-tutorial
• Can do:
• Line plots (use plot)
• Scatter plots (use scatter)
• Wireframe plots (use plot_wireframe)
• Surface plots (use plot_surface)
• Contours (use contour)
• Bar charts (use bar)
21
Overview
• Numpy: basic objects, methods, functions
• Numpy: linear algebra
• Numpy: random
• Matplotlib: 2D plots
• Matplotlib: 3D plots
• Scipy vs Numpy
• Discuss assignment 4
23
http://docs.scipy.org/doc/scipy/reference/
24
Overview
• Numpy: basic objects, methods, functions
• Numpy: linear algebra
• Numpy: random
• Matplotlib: 2D plots
• Matplotlib: 3D plots
• Scipy vs Numpy
• Discuss assignment 4
25
Assignment 4 discussion
• Your questions on assignment 4?