418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read...

60
418512: Computer Programming Languages Lecture 7 Pramook Khungurn

Transcript of 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read...

Page 1: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

418512: Computer Programming LanguagesLecture 7

Pramook Khungurn

Page 2: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Python Mathematical Libraries

• NumPy• SciPy• matplotlib

Page 3: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

NUMPY

Page 4: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Numpy

• Numpy manipulates homogeneous multidimensional array.– Table of same types of elements.– Indexed by tuple of positive integers.

• Example:– [1, 2, 1] rank 1 1 axis– [[ 1., 0., 0.], [ 0., 1., 2.]] rank 2 2 axis

Page 5: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

ndarray

• ndarray = class for multidim array• Properties– ndarray.ndim• number of axes

– ndarray.shape• tuple of integers indicating sizes of array in each dim

– ndarray.size• total number of elements in an array

Page 6: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

ndarray

• Properpies– ndarray.dtype• type of elements in the array

– ndarray.itemsize• size in bytes of each element of the array

– ndarray.data• memory containing the actual elements• don’t deal with this directly

Page 7: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

ndarray>>> from numpy import *>>> a = arange(10).reshape(2,5)>>> aarray([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])>>> a.shape(2, 5)>>> a.size10>>> a.dtypedtype('int32')>>> a.itemsize4

Page 8: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Array Creation• Use the “array” function.

>>> from numpy import *>>> a = array( [2,3,4] )>>> aarray([2, 3, 4])>>> type(a)<type 'numpy.ndarray'>

>>> b = array( [(1.5,2,3), (4,5,6)] )>>> barray([[ 1.5, 2. , 3. ], [ 4. , 5. , 6. ]])

Page 9: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

dtype parameter>>> c = array( [ [1,2], [3,4] ], dtype=complex )>>> carray([[ 1.+0.j, 2.+0.j], [ 3.+0.j, 4.+0.j]])

Page 10: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Array Creation

• zeros

>>> zeros((3,4))array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]])

Page 11: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Array Creation

• ones

>>> ones((2,3,4), dtype=int16)array([[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]],

[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]], dtype=int16)

Page 12: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Array Creation

• empty

>>> empty((2,3))array([[ 2.59345406e+161, 8.12549991e-096, 2.24630812e-057],

[ 1.12958007e+277, 2.21211602e+214, 6.00003876e+000]])

Page 13: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Array Creation

• arange

>>> arange( 10, 30, 5 )array([10, 15, 20, 25])>>> arange( 0, 2, 0.3 )array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])

Page 14: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Array Creation

• linspace– last argument = number of elements

>>> linspace( 0, 2, 9 )array([ 0., 0.25, 0.5, 0.75, 1., 1.25, 1.5, 1.75, 2. ])

Page 15: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Basic Operations• Arithmetic operators apply elementwise.

>>> a = array( [20, 30, 40, 50] )>>> b = arange( 4 )>>> c = a - b>>> carray([20, 29, 38, 47])>>> b**2array([0, 1, 4, 9])>>> 10*sin(a)array([ 9.12945251, -9.88031624, 7.4511316 , -2.62374854])

>>> a < 35array([ True, True, False, False], dtype=bool)

Page 16: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Multiplication

• * (multiplication) operates elementwise too.– Not matrix multiplication.

>>> A = array( [[1,1], [0,1]] )>>> B = array( [[2,0], [3,4]] )>>> A * Barray([[2, 0], [0, 4]])

Page 17: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Matrix Product

• Use the “dot” function.

>>> A = array( [[1,1], [0,1]] )>>> B = array( [[2,0], [3,4]] )>>> dot(A,B)array([[5, 4], [3, 4]])

Page 18: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

In-Place Operator• Some operators do not create new arrays.

>>> a = ones((2,3), dtype=int)>>> b = random.random((2,3))>>> a *= 3>>> aarray([[3, 3, 3], [3, 3, 3]])>>> b += a>>> barray([[ 3.61985813, 3.66556966, 3.87592214], [ 3.20532333, 3.23846057, 3.48698414]])>>> a += b>>> aarray([[6, 6, 6], [6, 6, 6]])

Page 19: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Unary Operators>>> a = random.random((2,3))>>> aarray([[ 0.5984133 , 0.93697558, 0.18051175], [ 0.81601338, 0.04679024, 0.73921303]])>>> a.sum()3.3179172722383647>>> a.min()0.046790239698870417>>> a.max()0.93697558122865876

Page 20: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

axis parameter• Supply the “axis” parameter to apply operators along a particular axis.

>>> b = arange(12).reshape(3,4)>>> barray([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])>>> b.sum(axis=0)array([12, 15, 18, 21])>>> b.min(axis=1)array([0, 4, 8])>>> b.cumsum(axis=1)array([[ 0, 1, 3, 6], [ 4, 9, 15, 22], [ 8, 17, 27, 38]])

Page 21: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Universal Functions

>>> B = arange(3)>>> Barray([0, 1, 2])>>> exp(B)array([ 1., 2.71828183, 7.3890561 ])>>> sqrt(B)array([ 0., 1., 1.41421356])

Page 22: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Indexing, Slicing, and Iterating>>> a = arange(10)**3>>> aarray([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])>>> a[2]8>>> a[2:5]array([ 8, 27, 64])>>> a[3] = -1000>>> aarray([0, 1, 8, -1000, 64, 125, 216, 343, 512, 729])

Page 23: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Indexing Multidimensional Array• Indices are tuple separated by commas.

>>> def f(x,y):... return 10*x+y...>>> b = fromfunction(f, (5,4), dtype=int)>>> barray([[ 0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33], [40, 41, 42, 43]])>>> b[2,3]23>>> b[:,1]array([ 1, 11, 21, 31, 41])>>> b[1:3,:]array([[10, 11, 12, 13], [20, 21, 22, 23]])

Page 24: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Iterating over Multidimensional Array

• You get the rows.

>>> for row in b:... print row...[0 1 2 3][10 11 12 13][20 21 22 23][30 31 32 33][40 41 42 43]

Page 25: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Flat Attribute

• Use “flat” attribute to get each element.

>>> for element in b.flat:... print element,...0 1 2 3 10 11 12 13 20 21 22 23 30 31 32 33 40 41 42 43

Page 26: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Ravel and Transpose>>> a = floor(10*random.random((3,4)))>>> aarray([[ 4., 0., 0., 0.], [ 7., 2., 1., 3.], [ 6., 9., 1., 1.]])

>>> a.ravel()array([ 4., 0., 0., 0., 7., 2., 1., 3., 6.,

9., 1., 1.])

>>> a.transpose()array([[ 4., 7., 6.], [ 0., 2., 9.], [ 0., 1., 1.], [ 0., 3., 1.]])

Page 27: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Reshape and Resize• Reshape returns a new array.

>>> a = floor(10*random.random((3,4)))

>>> a.reshape(6,2)array([[ 0., 7.], [ 6., 8.], [ 3., 5.], [ 3., 9.], [ 0., 7.], [ 4., 7.]])

>>> a.resize(2,6)>>> aarray([[ 0., 7., 6., 8., 3., 5.], [ 3., 9., 0., 7., 4., 7.]])

Page 28: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Deep Copy• Do deep copy with the “copy” method.

>>> a = arange(12).reshape(3,4)>>> aarray([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])>>> d = a.copy()>>> d is aFalse>>> d[0,0] = 9999>>> aarray([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])>>> darray([[9999, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])

Page 29: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Matrix• Matrices are created by “matrix” function.• Matrices are different from arrays that * is matrix multiplication.

>>> A = matrix([[1,2,3], [4,5,6], [7,8,9]])>>> Amatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])>>> B = matrix([[2,0,0], [0,2,0], [0,0,2]])>>> A * Bmatrix([[ 2, 4, 6], [ 8, 10, 12], [14, 16, 18]])

Page 30: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

SCIPY

Page 31: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Scipy

• Scipy is for scientific computation.• To use: import scipy

• Useful subpackages:– cluster clustering algorithms– fftpack fast fourier transform– integrate integration– interpolate interpolation and smoothing splines– linalg linear algebra– optimize optimization and root-finding– signal signal processing– sparse sparse matrices– stats statistical distribution and functions

Page 32: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Scipy Documentation

• Use “scipy.info” on packages or functions.

>>> import scipy>>> from scipy import linalg>>> scipy.info(linalg)

>>> scipy.info(linalg.det)

Page 33: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

integrate.quad

• Use it to integrate a general function.• For example, if you want to compute:

>>> from scipy import integrate>>> integrate.quad(lambda x: x**2, 0, 4.5)(30.375, 3.372302437298913e-13)

• The first return value is the value of integration.• The second is the upper bound on the error.

Z 4:5

0x2dx

Page 34: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

optimize.fmin

• Use it to find the argument that minimizes a function.

• For example, if you want to minimize:

>>> def rosen(x):... return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)...>>> x0 = [1.3, 0.7, 0.8, 1.9, 1.2]>>> xopt = fmin(rosen, x0, xtol=1e-8)>>> print xopt[ 1. 1. 1. 1. 1.]

f (x) =N ¡ 1X

i=1

100(xi ¡ x2i ¡ 1)2+(1¡ xi ¡ 1)2

Page 35: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

optimize.fsolve

• Use it to find a zero of a function.• Suppose you want to solve:

>>> def func(x):... return x + 2*cos(x)...>>> from scipy.optimize import fsolve>>> x0 = fsolve(func, 0.3)>>> print x0[-1.02986653]

x+2cos(x) = 0

Page 36: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

optimize.fsolve

• You want to solve a system of equation

>>> def func2(x):... out = [x[0]*cos(x[1])-4]... out.append(x[1]*x[0] - x[1] - 5)... return out...>>> x02 = fsolve(func2, [1,1])>>> print x02[ 6.50409711 0.90841421]

x0 cos(x1) = 4

x0x1 ¡ x1 =5

Page 37: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

linalg.inv

• Find the inverse of a matrix.

>>> A = matrix([[1,3,5], [2,5,1], [2,3,8]])

>>> from scipy import linalg>>> linalg.inv(A)array([[-1.48, 0.36, 0.88], [ 0.56, 0.08, -0.36], [ 0.16, -0.12, 0.04]])

Page 38: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

linalg.solve

• To solve the linear system:

>>> A = mat('[1 3 5; 2 5 1; 2 3 8]')>>> b = mat('[10;8;3]')>>> linalg.solve(A,b)array([[-9.28], [ 5.16], [ 0.76]])

Page 39: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

linalg.det

• Find the determinant.

>>> A = mat('[1 3 5; 2 5 1; 2 3 8]')>>> linalg.det(A)-25.000000000000004

Page 40: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

linalg.svd

• Find the singular value decomposition.

>>> A = mat('[1 3 2; 1 2 3]')>>> U, s, Vh = linalg.svd(A)>>> Uarray([[-0.70710678, -0.70710678], [-0.70710678, 0.70710678]])>>> sarray([ 5.19615242, 1. ])>>> Vharray([[ -2.72165527e-01, -6.80413817e-01, -6.80413817e-01], [ 2.42861287e-16, -7.07106781e-01, 7.07106781e-01], [ -9.62250449e-01, 1.92450090e-01, 1.92450090e-

01]])

Page 41: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

linalg.eig

• Find eigenvalues and eigenvectors.

>>> A = mat('[1 5 2; 2 4 1; 3 6 2]')>>> la, v = linalg.eig(A)>>> varray([[-0.5297175 , -0.90730751, 0.28380519], [-0.44941741, 0.28662547, -0.39012063], [-0.71932146, 0.30763439, 0.87593408]])>>> laarray([ 7.95791620+0.j, -1.25766471+0.j,

0.29974850+0.j])

Page 42: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

MATPLOTLIB

Page 43: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

matplotlib.pyplot

• A package for plotting graphs.• pyplot works like MATLAB.– Commands make changes a figure.– Stateful.

Page 44: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

pyplot.plot

>>> import matplotlib.pyplot as plt>>> plt.plot([1,2,3,4])[<matplotlib.lines.Line2D object at 0x02D370B0>]

>>> plt.ylabel('some numbers')<matplotlib.text.Text object at 0x02D3F250>>>> plt.show()

Page 45: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

pyplot.plot

Page 46: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

pyplot.plot

• Here’s how you plot x-vs-y.

Page 47: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

pyplot.plot

• Third argument indicates the style of the graph.– Default value ‘b-’ is blue line.– Below: ‘ro’ is red dots.

>>> plt.plot([1,2,3,4], [1,4,9,16], 'ro')[<matplotlib.lines.Line2D object at 0x02F382F0>]

>>> plt.show()

Page 48: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

pyplot.plot

Page 49: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

pyplot.axis

• axis() takes [xmin, xmax, ymin, ymax]

>>> plt.plot([1,2,3,4], [1,4,9,16], 'ro')>>> plt.axis([0, 6, 0, 20])>>> plt.show()

Page 50: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

pyplot.axis

Page 51: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

matplotlib and Numpy

• You can use Numpy arrays as arrays of x and y values.

>>> import numpy as np>>> import matplotlib.pyplot as plt>>> t = np.arange(0., 5., 0.2)>>> plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')>>> plt.show()

Page 52: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

matplotlib and Numpy

Page 53: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

pyplot.subplot

• subplot(r, c, fignum)– Create a grid with r rows and c cols.– fignum ranges from 1 to r*c.– fignum selects which grid cell to draw to.

Page 54: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

pyplot.subplot>>> 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(2,1,1)>>> plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')>>> plt.subplot(2,1,2)>>> plt.plot(t2, np.cos(2*np.pi*t2), 'r--')>>> plt.show()

Page 55: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

pyplot.subplot

Page 56: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Drawing Text

• text()– Add text at arbitrary location.

• xlabel(), ylabel(), title()– Add text at specified locations.

Page 57: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Drawing Textimport numpy as np import matplotlib.pyplot as plt

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

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)

Page 58: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

Drawing Text

Page 59: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

pyplot.annotate• Makes it easy to annotate the graph.

– xy: the location of to be annotated– xytext: the location of the text

ax = plt.subplot(111) t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2)

plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), )

plt.ylim(-2,2) plt.show()

Page 60: 418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.

pyplot.annotate