49
Introduction to Computing Plotting Functions with Python (Lecture 8) 9 October, 2019

Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Introduction to Computing

Plotting Functions with Python (Lecture 8)

9 October, 2019

Page 2: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Numpy

We shall take a detour in this class to learn plotting functions usingPython.

For this we shall need to know a bit about NumPy arrays whichare much faster than the built-in Python lists.

NumPy is the fundamental package for scientific computing inPython.

It (NumPy) is a Python library that provides a multidimensionalarray object, (like a built-in data type), and various derived objects.

To use the NumPy library we have to import it just like any otherPython Library.

>>> import numpy as np

Page 3: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Numpy

We shall take a detour in this class to learn plotting functions usingPython.

For this we shall need to know a bit about NumPy arrays whichare much faster than the built-in Python lists.

NumPy is the fundamental package for scientific computing inPython.

It (NumPy) is a Python library that provides a multidimensionalarray object, (like a built-in data type), and various derived objects.

To use the NumPy library we have to import it just like any otherPython Library.

>>> import numpy as np

Page 4: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Numpy

We shall take a detour in this class to learn plotting functions usingPython.

For this we shall need to know a bit about NumPy arrays whichare much faster than the built-in Python lists.

NumPy is the fundamental package for scientific computing inPython.

It (NumPy) is a Python library that provides a multidimensionalarray object, (like a built-in data type), and various derived objects.

To use the NumPy library we have to import it just like any otherPython Library.

>>> import numpy as np

Page 5: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Numpy

We shall take a detour in this class to learn plotting functions usingPython.

For this we shall need to know a bit about NumPy arrays whichare much faster than the built-in Python lists.

NumPy is the fundamental package for scientific computing inPython.

It (NumPy) is a Python library that provides a multidimensionalarray object, (like a built-in data type), and various derived objects.

To use the NumPy library we have to import it just like any otherPython Library.

>>> import numpy as np

Page 6: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Numpy

We shall take a detour in this class to learn plotting functions usingPython.

For this we shall need to know a bit about NumPy arrays whichare much faster than the built-in Python lists.

NumPy is the fundamental package for scientific computing inPython.

It (NumPy) is a Python library that provides a multidimensionalarray object, (like a built-in data type), and various derived objects.

To use the NumPy library we have to import it just like any otherPython Library.

>>> import numpy as np

Page 7: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Numpy Array

NumPy has a built-in datatype called array.

The easiest way to create a NumPy array is from a Python list usingthe array function in NumPy library.

>>> import numpy as np>>> b = np.array([6, 7, 8])>>> barray([6, 7, 8])>>> type(b)<class 'numpy.ndarray'>

Page 8: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Numpy Array

NumPy has a built-in datatype called array.

The easiest way to create a NumPy array is from a Python list usingthe array function in NumPy library.

>>> import numpy as np>>> b = np.array([6, 7, 8])>>> barray([6, 7, 8])>>> type(b)<class 'numpy.ndarray'>

Page 9: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Numpy Array

NumPy has a built-in datatype called array.

The easiest way to create a NumPy array is from a Python list usingthe array function in NumPy library.

>>> import numpy as np>>> b = np.array([6, 7, 8])>>> barray([6, 7, 8])>>> type(b)<class 'numpy.ndarray'>

Page 10: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Numpy Array

Another way to create an array is using the arange function inNumPy, which is just like the range function in Python.

>>> import numpy as np>>> c = np.arange(0, 2*np.pi, np.pi/4)>>> carray([ 0. , 0.78539816, 1.57079633,

2.35619449, 3.14159265, 3.92699082,4.71238898, 5.49778714])

The function arange takes 3 arguments, start, stop, step,these could be integers or floats. It returns a NumPy array withstart as the first element, start + step as the second element,start + step + step as the third element and so on. The lastelement is the largest such number strictly smaller than stop.

Page 11: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Numpy Array

Another way to create an array is using the arange function inNumPy, which is just like the range function in Python.

>>> import numpy as np>>> c = np.arange(0, 2*np.pi, np.pi/4)>>> carray([ 0. , 0.78539816, 1.57079633,

2.35619449, 3.14159265, 3.92699082,4.71238898, 5.49778714])

The function arange takes 3 arguments, start, stop, step,these could be integers or floats. It returns a NumPy array withstart as the first element, start + step as the second element,start + step + step as the third element and so on. The lastelement is the largest such number strictly smaller than stop.

Page 12: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Numpy Array

Another way to create an array is using the arange function inNumPy, which is just like the range function in Python.

>>> import numpy as np>>> c = np.arange(0, 2*np.pi, np.pi/4)>>> carray([ 0. , 0.78539816, 1.57079633,

2.35619449, 3.14159265, 3.92699082,4.71238898, 5.49778714])

The function arange takes 3 arguments, start, stop, step,these could be integers or floats. It returns a NumPy array withstart as the first element, start + step as the second element,start + step + step as the third element and so on. The lastelement is the largest such number strictly smaller than stop.

Page 13: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

More examples using arange

The arguments start and step are optional.

>>> np.arange(3)array([0, 1, 2])

>>> np.arange(3.0)array([ 0., 1., 2.])

>>> np.arange(3,7)array([3, 4, 5, 6])

>>> np.arange(3,7,2)array([3, 5])

Page 14: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

NumPy LinspaceIf you want to create an array by partitioning an interval into certainnumber of equal parts, then the linspace function in NumPy ismore convenient.

>>> np.linspace(0, 2*np.pi, 10)array([ 0. , 0.6981317 , 1.3962634 ,

2.0943951 , 2.7925268 , 3.4906585 ,4.1887902 , 4.88692191, 5.58505361,6.28318531])

numpy.linspace(start, stop, num) returns num evenly spacedsamples, calculated over the interval [start, stop]. The endpointof the interval is by default included.

>>> np.linspace(2.0, 3.0, num=5)array([ 2. , 2.25, 2.5 , 2.75, 3. ])

Page 15: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

NumPy LinspaceIf you want to create an array by partitioning an interval into certainnumber of equal parts, then the linspace function in NumPy ismore convenient.

>>> np.linspace(0, 2*np.pi, 10)array([ 0. , 0.6981317 , 1.3962634 ,

2.0943951 , 2.7925268 , 3.4906585 ,4.1887902 , 4.88692191, 5.58505361,6.28318531])

numpy.linspace(start, stop, num) returns num evenly spacedsamples, calculated over the interval [start, stop]. The endpointof the interval is by default included.

>>> np.linspace(2.0, 3.0, num=5)array([ 2. , 2.25, 2.5 , 2.75, 3. ])

Page 16: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

NumPy LinspaceIf you want to create an array by partitioning an interval into certainnumber of equal parts, then the linspace function in NumPy ismore convenient.

>>> np.linspace(0, 2*np.pi, 10)array([ 0. , 0.6981317 , 1.3962634 ,

2.0943951 , 2.7925268 , 3.4906585 ,4.1887902 , 4.88692191, 5.58505361,6.28318531])

numpy.linspace(start, stop, num) returns num evenly spacedsamples, calculated over the interval [start, stop]. The endpointof the interval is by default included.

>>> np.linspace(2.0, 3.0, num=5)array([ 2. , 2.25, 2.5 , 2.75, 3. ])

Page 17: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Multi dimensional arrays

So far we have only seen one dimenstional arrays. But arrays can bemulti dimenstional.

I A one dimensional array is like a list of elements (numbers).I A two dimensional array is like a list of lists or a matrix of

numbers.I A three dimensional array is like a list of list of lists, and so on

We shall only need one and two dimensional arrays so we restrictour discussion to them.

Page 18: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Multi dimensional arrays

So far we have only seen one dimenstional arrays. But arrays can bemulti dimenstional.

I A one dimensional array is like a list of elements (numbers).I A two dimensional array is like a list of lists or a matrix of

numbers.I A three dimensional array is like a list of list of lists, and so on

We shall only need one and two dimensional arrays so we restrictour discussion to them.

Page 19: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Multi dimensional arrays

So far we have only seen one dimenstional arrays. But arrays can bemulti dimenstional.

I A one dimensional array is like a list of elements (numbers).I A two dimensional array is like a list of lists or a matrix of

numbers.I A three dimensional array is like a list of list of lists, and so on

We shall only need one and two dimensional arrays so we restrictour discussion to them.

Page 20: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Multidimensional arrays

Here ar esome examples of two dimensional arrays and how tocreate them.

>>> a = np.arange(15)>>> b = a.reshape((3,5))>>> aarray([ 0, 1, 2, 3, 4, 5, 6, 7,

8, 9, 10, 11, 12, 13, 14])>>> barray([[ 0, 1, 2, 3, 4],

[ 5, 6, 7, 8, 9],[10, 11, 12, 13, 14]])

Page 21: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Multidimensional arrays

Here ar esome examples of two dimensional arrays and how tocreate them.

>>> a = np.arange(15)>>> b = a.reshape((3,5))>>> aarray([ 0, 1, 2, 3, 4, 5, 6, 7,

8, 9, 10, 11, 12, 13, 14])>>> barray([[ 0, 1, 2, 3, 4],

[ 5, 6, 7, 8, 9],[10, 11, 12, 13, 14]])

Page 22: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Multidimensional arrays

Here are some more examples:

>>> a.reshape((5,3))array([[ 0, 1, 2],

[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11],[12, 13, 14]])

>>> c = np.array([[1, 4], [9, 16]])>>> carray([[ 1, 4],

[ 9, 16]])

Page 23: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Mathematical operations on NumPy arraysArithmetic operators on arrays apply elementwise. A new array iscreated and filled with the result.

>>> a = np.arange(10)>>> b = a.reshape((2,5))

>>> 2 * aarray([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])

>>> b ** 2array([[ 0, 1, 4, 9, 16],

[25, 36, 49, 64, 81]])

>>> a + 3array([ 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

>>> b - 4array([[-4, -3, -2, -1, 0],

[ 1, 2, 3, 4, 5]])

Page 24: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Mathematical operations on NumPy arrays

Two arrays can be added, subtracted, multiplied or divided only ifthey have the same dimensions. Again in that case the operation isdone element wise.

>>> a + bTraceback (most recent call last):

File "<stdin>", line 1, in <module>ValueError: operands could not be broadcasttogether with shapes (10,) (2,5)

Examples:

>>> a = np.arange(1,5)>>> b = a.reshape((2,2))

Page 25: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Mathematical operations on NumPy arrays

Two arrays can be added, subtracted, multiplied or divided only ifthey have the same dimensions. Again in that case the operation isdone element wise.

>>> a + bTraceback (most recent call last):

File "<stdin>", line 1, in <module>ValueError: operands could not be broadcasttogether with shapes (10,) (2,5)

Examples:

>>> a = np.arange(1,5)>>> b = a.reshape((2,2))

Page 26: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Examples

>>> barray([[1, 2],

[3, 4]])

>>> b**2 + barray([[ 2, 6],

[12, 20]])

>>> b**2 - barray([[ 0, 2],

[ 6, 12]])

>>> c = np.linspace(0,1,4).reshape((2,2))>>> carray([[ 0. , 0.33333333],

[ 0.66666667, 1. ]])

Page 27: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Examples

>>> c*barray([[ 0. , 0.66666667],

[ 2. , 4. ]])

>>> c/barray([[ 0. , 0.16666667],

[ 0.22222222, 0.25 ]])

>>> c*(b + b**2)array([[ 0., 2.],

[ 8., 20.]])

Page 28: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Applying functions to arraysNumPy has some built-in functions that can be applied to arrays.These functions act elementwise.

I Trigonometric functions: numpy.sin, numpy.cos,numpy.tan etc.

I Exponential and logarithm: numpy.exp, numpy.log,numpy.log10.

I Inverse trigonometric: numpy.arcsin, numpy.arccos,numpy.arctan.

I Roots: numpy.sqrt, numpy.cbrt.

I Round off functions: numpy.floor, numpy.ceil,numpy.round

I Absolute value: numpy.fabs.

Page 29: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Applying functions to arraysNumPy has some built-in functions that can be applied to arrays.These functions act elementwise.

I Trigonometric functions: numpy.sin, numpy.cos,numpy.tan etc.

I Exponential and logarithm: numpy.exp, numpy.log,numpy.log10.

I Inverse trigonometric: numpy.arcsin, numpy.arccos,numpy.arctan.

I Roots: numpy.sqrt, numpy.cbrt.

I Round off functions: numpy.floor, numpy.ceil,numpy.round

I Absolute value: numpy.fabs.

Page 30: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Applying functions to arraysNumPy has some built-in functions that can be applied to arrays.These functions act elementwise.

I Trigonometric functions: numpy.sin, numpy.cos,numpy.tan etc.

I Exponential and logarithm: numpy.exp, numpy.log,numpy.log10.

I Inverse trigonometric: numpy.arcsin, numpy.arccos,numpy.arctan.

I Roots: numpy.sqrt, numpy.cbrt.

I Round off functions: numpy.floor, numpy.ceil,numpy.round

I Absolute value: numpy.fabs.

Page 31: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Applying functions to arraysNumPy has some built-in functions that can be applied to arrays.These functions act elementwise.

I Trigonometric functions: numpy.sin, numpy.cos,numpy.tan etc.

I Exponential and logarithm: numpy.exp, numpy.log,numpy.log10.

I Inverse trigonometric: numpy.arcsin, numpy.arccos,numpy.arctan.

I Roots: numpy.sqrt, numpy.cbrt.

I Round off functions: numpy.floor, numpy.ceil,numpy.round

I Absolute value: numpy.fabs.

Page 32: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Applying functions to arraysNumPy has some built-in functions that can be applied to arrays.These functions act elementwise.

I Trigonometric functions: numpy.sin, numpy.cos,numpy.tan etc.

I Exponential and logarithm: numpy.exp, numpy.log,numpy.log10.

I Inverse trigonometric: numpy.arcsin, numpy.arccos,numpy.arctan.

I Roots: numpy.sqrt, numpy.cbrt.

I Round off functions: numpy.floor, numpy.ceil,numpy.round

I Absolute value: numpy.fabs.

Page 33: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Applying functions to arraysNumPy has some built-in functions that can be applied to arrays.These functions act elementwise.

I Trigonometric functions: numpy.sin, numpy.cos,numpy.tan etc.

I Exponential and logarithm: numpy.exp, numpy.log,numpy.log10.

I Inverse trigonometric: numpy.arcsin, numpy.arccos,numpy.arctan.

I Roots: numpy.sqrt, numpy.cbrt.

I Round off functions: numpy.floor, numpy.ceil,numpy.round

I Absolute value: numpy.fabs.

Page 34: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Applying functions to arraysNumPy has some built-in functions that can be applied to arrays.These functions act elementwise.

I Trigonometric functions: numpy.sin, numpy.cos,numpy.tan etc.

I Exponential and logarithm: numpy.exp, numpy.log,numpy.log10.

I Inverse trigonometric: numpy.arcsin, numpy.arccos,numpy.arctan.

I Roots: numpy.sqrt, numpy.cbrt.

I Round off functions: numpy.floor, numpy.ceil,numpy.round

I Absolute value: numpy.fabs.

Page 35: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Examples

>>> a = np.linspace(0, 2*np.pi, 13)>>> np.rad2deg(a)array([ 0., 30., 60., 90., 120.,

150., 180., 210., 240.,270., 300., 330., 360.])

>>> b = np.sin(a)>>> np.round(b,3)array([ 0. , 0.5 , 0.866, 1. , 0.866, 0.5 ,

0. , -0.5 , -0.866, -1. , -0.866, -0.5 ,-0. ])

>>> np.fabs(np.round(b,5))array([ 0. , 0.5 , 0.86603, 1. , 0.86603,

0.5 , 0. , 0.5 , 0.86603, 1. ,0.86603, 0.5 , 0. ])

Page 36: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Plotting functions

Now we shall be able to plot some function in python. For this weshall use the powerful Python library matplotlib.

>>> import matplotlib.pyplot as plt>>> import numpy as np>>> x = np.arange(1,5)>>> plt.plot(x, x**2)>>> plt.show()

Page 37: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

The Plot

Figure 1: Squares

Page 38: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Matplotlib and Pyplot

Pyplot is a library inside matplotlib.

The function plot in pyplot is used as follows:

If x and y are numpy arrays of the same length then to plot xversus y simply use plt.plot(x,y).

The function show in pyplot displays a figure. When running inipython with its pylab mode, it displays all figures and returns to theipython prompt.

In non-interactive mode, it displays all figures and blocks until thefigures have been closed.

Page 39: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Matplotlib and Pyplot

Pyplot is a library inside matplotlib.

The function plot in pyplot is used as follows:

If x and y are numpy arrays of the same length then to plot xversus y simply use plt.plot(x,y).

The function show in pyplot displays a figure. When running inipython with its pylab mode, it displays all figures and returns to theipython prompt.

In non-interactive mode, it displays all figures and blocks until thefigures have been closed.

Page 40: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Matplotlib and Pyplot

Pyplot is a library inside matplotlib.

The function plot in pyplot is used as follows:

If x and y are numpy arrays of the same length then to plot xversus y simply use plt.plot(x,y).

The function show in pyplot displays a figure. When running inipython with its pylab mode, it displays all figures and returns to theipython prompt.

In non-interactive mode, it displays all figures and blocks until thefigures have been closed.

Page 41: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

The plot function

The plot function plots the data point from the arrays x and y andjoins those points by a straight line.

It takes a third argument which is a format string that specifies

I the color of the graph: ‘r’ = red, ‘g’ = green, ‘b’ = blue, ‘k’ =black, ‘y’ = yellow;

I the marker for the points: ‘.’ = dot, ‘o’ =circle, ‘x’ =cross.I line style: ‘-’ = solid line, ‘–’ = dashed line, ‘:’= dotted line.

So the string "r.-" means the points in the graph will be plottedwith red dot markers joined by a solid red line.

Page 42: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

The plot function

The plot function plots the data point from the arrays x and y andjoins those points by a straight line.

It takes a third argument which is a format string that specifies

I the color of the graph: ‘r’ = red, ‘g’ = green, ‘b’ = blue, ‘k’ =black, ‘y’ = yellow;

I the marker for the points: ‘.’ = dot, ‘o’ =circle, ‘x’ =cross.I line style: ‘-’ = solid line, ‘–’ = dashed line, ‘:’= dotted line.

So the string "r.-" means the points in the graph will be plottedwith red dot markers joined by a solid red line.

Page 43: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

The plot function

The plot function plots the data point from the arrays x and y andjoins those points by a straight line.

It takes a third argument which is a format string that specifies

I the color of the graph: ‘r’ = red, ‘g’ = green, ‘b’ = blue, ‘k’ =black, ‘y’ = yellow;

I the marker for the points: ‘.’ = dot, ‘o’ =circle, ‘x’ =cross.I line style: ‘-’ = solid line, ‘–’ = dashed line, ‘:’= dotted line.

So the string "r.-" means the points in the graph will be plottedwith red dot markers joined by a solid red line.

Page 44: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Some better examples

>>> import matplotlib.pyplot as plt>>> import numpy as np

>>> x = np.linspace(0,1,100)

>>> plt.plot(x, x**2, 'b-')>>> plt.plot(x, np.sqrt(x), 'b:')

>>> plt.plot(x, x**3, 'r-')>>> plt.plot(x, np.cbrt(x), 'r:')

>>> plt.show()

Page 45: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Graph

Figure 2: Powers

Page 46: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Damped Oscilation

>>> import matplotlib.pyplot as plt>>> import numpy as np

>>> x = np.linspace(0, 10*np.pi, 1000)>>> y = (1/(x+1))*np.cos(x)

>>> plt.plot(x, y)

>>> plt.plot(x, 1/(x+1), 'y:')>>> plt.plot(x, -1/(x+1), 'y:')

>>> plt.show()

Page 47: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Graph

Figure 3: Damped Oscilation

Page 48: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Graph of sin(1x )

>>> import matplotlib.pyplot as plt>>> import numpy as np

>>> x = np.linspace(0,1, 500)>>> x = 1/(10*np.pi) + x

>>> plt.plot(x, np.sin(1/x))

>>> plt.show()

Page 49: Introduction to Computingchitrabhanu/intro_to_computing/...Introduction to Computing Author Plotting Functions with Python (Lecture 8) Created Date 10/11/2019 10:22:55 AM

Graph

Figure 4: Graph of sin(1/x)