lecture 12: es 102 plotting with matplotlib · lecture 12: es 102 plotting with matplotlib...

31
logo Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 1 / 22

Upload: others

Post on 09-Oct-2020

38 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Lecture 12: ES 102Plotting With Matplotlib

Introduction to Computing

IIT Gandhinagar, India

October 29, 2013

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 1 / 22

Page 2: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Simple Plotting

Matplotlib provides the necessary functions for data visualization.pylab is an interface to matplotlib.

import pylab as pl to use matplotlib or from pylab import *.

Matplotlib is generally used in conjunction with numpy.

Numpy is already loaded in pylab and np (so you don’t need import

numpy as np).

This lecture is based on http://scipy-lectures.github.io/

intro/matplotlib/matplotlib.html.

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 2 / 22

Page 3: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Simple Plotting

Matplotlib provides the necessary functions for data visualization.pylab is an interface to matplotlib.

import pylab as pl to use matplotlib or from pylab import *.

Matplotlib is generally used in conjunction with numpy.

Numpy is already loaded in pylab and np (so you don’t need import

numpy as np).

This lecture is based on http://scipy-lectures.github.io/

intro/matplotlib/matplotlib.html.

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 2 / 22

Page 4: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Simple Plotting

Matplotlib provides the necessary functions for data visualization.pylab is an interface to matplotlib.

import pylab as pl to use matplotlib or from pylab import *.

Matplotlib is generally used in conjunction with numpy.

Numpy is already loaded in pylab and np (so you don’t need import

numpy as np).

This lecture is based on http://scipy-lectures.github.io/

intro/matplotlib/matplotlib.html.

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 2 / 22

Page 5: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Simple Plotting

Matplotlib provides the necessary functions for data visualization.pylab is an interface to matplotlib.

import pylab as pl to use matplotlib or from pylab import *.

Matplotlib is generally used in conjunction with numpy.

Numpy is already loaded in pylab and np (so you don’t need import

numpy as np).

This lecture is based on http://scipy-lectures.github.io/

intro/matplotlib/matplotlib.html.

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 2 / 22

Page 6: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Simple Plotting

Matplotlib provides the necessary functions for data visualization.pylab is an interface to matplotlib.

import pylab as pl to use matplotlib or from pylab import *.

Matplotlib is generally used in conjunction with numpy.

Numpy is already loaded in pylab and np (so you don’t need import

numpy as np).

This lecture is based on http://scipy-lectures.github.io/

intro/matplotlib/matplotlib.html.

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 2 / 22

Page 7: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Simple Plot

from py l ab import ∗ # This impo r t s ma t p l o t l i b f u n c t i o n s and numpy as np

X=np . a range(−np . p i , np . p i , 2∗ np . p i /256)# Crea t e s 256 un i fo rmed spaced numbers between −p i to p i

S=np . s i n (X) # cos ( x ) , s i n ( x )p l o t (X, S) #p l o t c r e a t e s an o b j e c t which can be p l o t t e d

x l a b e l ( ’x’ , f o n t s i z e =14, c o l o r=’blue’ ) #l a b e l a x i sy l a b e l ( ’sin(x)’ )t i t l e ( ’Plot of sin(x)’ )show ( ) #This a c t u a l l y p l o t s the graph

L12Programs/simpleplot.py

plot() generates an object that can be plotted.

xlabel(’’),ylabel(’’) labels to the X and Y axis

title(’’) title of the plot

show() actually draws all the plottable objects created.

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 3 / 22

Page 8: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Simple Plot

from py l ab import ∗ # This impo r t s ma t p l o t l i b f u n c t i o n s and numpy as np

X=np . a range(−np . p i , np . p i , 2∗ np . p i /256)# Crea t e s 256 un i fo rmed spaced numbers between −p i to p i

S=np . s i n (X) # cos ( x ) , s i n ( x )p l o t (X, S) #p l o t c r e a t e s an o b j e c t which can be p l o t t e d

x l a b e l ( ’x’ , f o n t s i z e =14, c o l o r=’blue’ ) #l a b e l a x i sy l a b e l ( ’sin(x)’ )t i t l e ( ’Plot of sin(x)’ )show ( ) #This a c t u a l l y p l o t s the graph

L12Programs/simpleplot.py

plot() generates an object that can be plotted.

xlabel(’’),ylabel(’’) labels to the X and Y axis

title(’’) title of the plot

show() actually draws all the plottable objects created.

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 3 / 22

Page 9: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Simple Plot

from py l ab import ∗ # This impo r t s ma t p l o t l i b f u n c t i o n s and numpy as np

X=np . a range(−np . p i , np . p i , 2∗ np . p i /256)# Crea t e s 256 un i fo rmed spaced numbers between −p i to p i

S=np . s i n (X) # cos ( x ) , s i n ( x )p l o t (X, S) #p l o t c r e a t e s an o b j e c t which can be p l o t t e d

x l a b e l ( ’x’ , f o n t s i z e =14, c o l o r=’blue’ ) #l a b e l a x i sy l a b e l ( ’sin(x)’ )t i t l e ( ’Plot of sin(x)’ )show ( ) #This a c t u a l l y p l o t s the graph

L12Programs/simpleplot.py

plot() generates an object that can be plotted.

xlabel(’’),ylabel(’’) labels to the X and Y axis

title(’’) title of the plot

show() actually draws all the plottable objects created.

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 3 / 22

Page 10: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Simple Plot

from py l ab import ∗ # This impo r t s ma t p l o t l i b f u n c t i o n s and numpy as np

X=np . a range(−np . p i , np . p i , 2∗ np . p i /256)# Crea t e s 256 un i fo rmed spaced numbers between −p i to p i

S=np . s i n (X) # cos ( x ) , s i n ( x )p l o t (X, S) #p l o t c r e a t e s an o b j e c t which can be p l o t t e d

x l a b e l ( ’x’ , f o n t s i z e =14, c o l o r=’blue’ ) #l a b e l a x i sy l a b e l ( ’sin(x)’ )t i t l e ( ’Plot of sin(x)’ )show ( ) #This a c t u a l l y p l o t s the graph

L12Programs/simpleplot.py

plot() generates an object that can be plotted.

xlabel(’’),ylabel(’’) labels to the X and Y axis

title(’’) title of the plot

show() actually draws all the plottable objects created.

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 3 / 22

Page 11: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Simple plot output

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 4 / 22

Page 12: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Customizing

from py l ab import ∗

x=np . l i n s p a c e (−np . p i , np . p i , 200 , endpo in t=True )

s = np . s i n ( x )c = np . cos ( x )p l o t ( x , s , c o l o r=’red’ , l i n e w i d t h =1, l i n e s t y l e=’--’ , l a b e l="sine" )p l o t ( x , c , c o l o r=’blue’ , l i n e w i d t h =1.5 , l i n e s t y l e=’-’ , l a b e l="cosine" )l e g end ( l o c=’upper right’ )

show ( )

L12Programs/custom plot.py

linspace(start,end,no of elms,endpoint=True/False):generate no of elms many equally spaced elements from start toend. end will not be included only if endpoint=False.

color color of the curve;linewidth thickness of thecurve;linestyle curve pattern; label set the name of the curve.

legend where to show the names of the curves.

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 5 / 22

Page 13: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Customizing output

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 6 / 22

Page 14: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Customizing: Axes Limit, Ticks and Gridimport numpy as npimport py l ab as p l# Note t h i s t ime we a r e impo r t i n g p y l a s b as p l

x=np . l i n s p a c e (−2∗np . p i , 2∗ np . p i , 200 , endpo in t=True )

s = np . s i n ( x )p l . p l o t ( x , s , c o l o r=’green’ , l i n e w i d t h =1, l i n e s t y l e=’-’ )

p l . x l im ( x . min ( ) ∗ 1 . 1 , x . max ( ) ∗ 1 . 1 )p l . y l im ( −1 .1 ,1 .1 )

p l . x t i c k s ([−2∗np . p i , −np . p i , 0 , np . p i , 2∗np . p i ] )p l . y t i c k s ([−1 , −0 .5 ,0 ,0 .5 , +1])

p l . g r i d ( True )p l . show ( )

L12Programs/limticks.py

xlim sets x-axis limit, ylim sets y-axis limit.xticks,yticks puts the ticks in the axes.grid sets a rectangular grid.

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 7 / 22

Page 15: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Customizing: Axes limit etc.. output

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 8 / 22

Page 16: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

More Customizingimport numpy as npimport py l ab as p l

x=np . l i n s p a c e (−2∗np . p i , 2∗ np . p i , 200 , endpo in t=True )

s = np . s i n ( x )c=np . cos ( x )p l . p l o t ( x , s , c o l o r=’green’ , l i n e w i d t h =1, l i n e s t y l e=’-’ )p l . x l a b e l ( ’angle $\\ theta$ ’ , f o n t s i z e =14, c o l o r=’red’ )p l . y l a b e l ( ’cos($\\ theta$) and sin($\\ theta$)’ )

p l . x t i c k s ([−2∗np . p i , −np . p i , 0 , np . p i , 2∗np . p i ] ,[ r ’-$2\pi$’ , r ’$\pi$’ , r ’$0$’ , r ’$\pi$’ , r ’$2\pi$’ ] )

p l . y t i c k s ([−1 , 0 , +1])

p l . show ( )

L12Programs/latex embed.py

We can embed some LATEXcode in the program to get fancy fonts.

Greek Characters like : α, β, π, θ can be printed

even equations dydx |(x0, y0) can be embedded

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 9 / 22

Page 17: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

More Customizingimport numpy as npimport py l ab as p l

x=np . l i n s p a c e (−2∗np . p i , 2∗ np . p i , 200 , endpo in t=True )

s = np . s i n ( x )c=np . cos ( x )p l . p l o t ( x , s , c o l o r=’green’ , l i n e w i d t h =1, l i n e s t y l e=’-’ )p l . x l a b e l ( ’angle $\\ theta$ ’ , f o n t s i z e =14, c o l o r=’red’ )p l . y l a b e l ( ’cos($\\ theta$) and sin($\\ theta$)’ )

p l . x t i c k s ([−2∗np . p i , −np . p i , 0 , np . p i , 2∗np . p i ] ,[ r ’-$2\pi$’ , r ’$\pi$’ , r ’$0$’ , r ’$\pi$’ , r ’$2\pi$’ ] )

p l . y t i c k s ([−1 , 0 , +1])

p l . show ( )

L12Programs/latex embed.py

We can embed some LATEXcode in the program to get fancy fonts.

Greek Characters like : α, β, π, θ can be printed

even equations dydx |(x0, y0) can be embedded

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 9 / 22

Page 18: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

More Customizingimport numpy as npimport py l ab as p l

x=np . l i n s p a c e (−2∗np . p i , 2∗ np . p i , 200 , endpo in t=True )

s = np . s i n ( x )c=np . cos ( x )p l . p l o t ( x , s , c o l o r=’green’ , l i n e w i d t h =1, l i n e s t y l e=’-’ )p l . x l a b e l ( ’angle $\\ theta$ ’ , f o n t s i z e =14, c o l o r=’red’ )p l . y l a b e l ( ’cos($\\ theta$) and sin($\\ theta$)’ )

p l . x t i c k s ([−2∗np . p i , −np . p i , 0 , np . p i , 2∗np . p i ] ,[ r ’-$2\pi$’ , r ’$\pi$’ , r ’$0$’ , r ’$\pi$’ , r ’$2\pi$’ ] )

p l . y t i c k s ([−1 , 0 , +1])

p l . show ( )

L12Programs/latex embed.py

We can embed some LATEXcode in the program to get fancy fonts.

Greek Characters like : α, β, π, θ can be printed

even equations dydx |(x0, y0) can be embedded

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 9 / 22

Page 19: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

More customizing : output

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 10 / 22

Page 20: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Bar Plot

from py l ab import ∗

x=np . a r r a y ( [ 1 , 2 , 3 , 4 , 5 , 6 ] )

y=np . a r r a y ( [ 5 , 2 , 7 , 9 , 1 1 , 3 ] )

z=np . random . rand (6)∗10

bar ( x , y , f a c e c o l o r=’blue’ , e d g e c o l o r=’white’ )bar ( x ,−z , f a c e c o l o r=’red’ , e d g e c o l o r=’white’ )

y l a b e l ( ’Barplot ’ )t i t l e ( ’Example of a barplot ’ )show ( )

L12Programs/barplot ex.py

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 11 / 22

Page 21: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Bar Plot : output

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 12 / 22

Page 22: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Scatter Plot

from py l ab import ∗

n = 256X=np . l i n s p a c e (−2∗np . p i , 2∗ np . p i , n , endpo in t=True )Y = s i n (X)+0.1∗np . random . normal (0 , 1 , n )

print X. shape , Y . shape

s c a t t e r (X,Y)

show ( )

L12Programs/scatter ex1.py

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 13 / 22

Page 23: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Scatter Plot : output

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 14 / 22

Page 24: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Polar Plot

from py l ab import ∗

axes ( p o l a r=True ) #This i s the on l y change you need to do .

N=200

th e t a=np . l i n s p a c e (0 ,2∗ np . p i ,N, endpo in t=True )r a d i i =10∗np . s i n ( t h e t a )r a d i i 2 =2∗ t h e t a

p l o t ( theta , r a d i i , c o l o r=’red’ )

p l o t ( theta , r a d i i 2 , c o l o r=’blue’ )

show ( )

L12Programs/polar ex.py

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 15 / 22

Page 25: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Polar Plot : output

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 16 / 22

Page 26: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

subplotsfrom py l ab import ∗s u bp l o t ( 2 , 2 , 1 ) # Crea t e s a mat r i x o f 2x2 p l o t sX=np . l i n s p a c e (−1 ,1 ,256 , endpo in t=True )

Y1 , Y2 , Y3 , Y4=X, np . power (X, 2 ) , np . power (X, 3 ) , np . power (X, 4 )

p l o t (X, Y1 , c o l o r=’blue’ , l a b e l=’$x$’ )l e g=l egend ( l o c=’best’ )

s u bp l o t ( 2 , 2 , 2 )p l o t (X, Y2 , c o l o r=’red’ , l a b e l=’$x^2$’ )l e g end ( l o c=’best’ )

s u bp l o t ( 2 , 2 , 3 )p l o t (X, Y3 , c o l o r=’green’ , l a b e l=’$x^3$’ )x l a b e l ( ’x’ )l e g end ( l o c=’best’ )

s u bp l o t ( 2 , 2 , 4 )p l o t (X, Y4 , c o l o r=’black’ , l a b e l=’$x^4$’ )x l a b e l ( ’x’ )l e g end ( l o c=’best’ )

show ( )

L12Programs/subplot ex.pyShivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 17 / 22

Page 27: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

subplots : output

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 18 / 22

Page 28: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Plotting Data from FileIn many situations the data to be plotted is in a file.You can read the data, store it in arrays and plot the data.

from py l ab import ∗

f o=open ( ’height_weight_data.txt’ )

temp=fo . r e a d l i n e ( )heade r=temp . s p l i t ( )print heade r

x=[]y =[]

for l i n e in f o :temp=l i n e . s p l i t ( )i nd ex=temp [ 0 ]h e i g h t=temp [ 1 ]we ight=temp [ 2 ]x+=[h e i g h t ]y+=[we ight ]

X=np . a r r a y ( x )Y=np . a r r a y ( y )

print X. shapeprint Y. shapep l o t (X,Y, c o l o r=’blue’ , marker=’o’ , l i n e s t y l e=’None’ )x l a b e l ( heade r [ 1 ] )y l a b e l ( heade r [ 2 ] )

show ( )

L12Programs/data plot.pyShivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 19 / 22

Page 29: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Data Plot : output

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 20 / 22

Page 30: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Animation

Use animation package import matplotlib.animation as

animation

Ex1: L12Programs/animate basic example.py

Ex2: L12*Programs/animate double pendulum.py

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 21 / 22

Page 31: Lecture 12: ES 102 Plotting With Matplotlib · Lecture 12: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India October 29, 2013 Shivakumar,Bireswar (IIT

logo

Practice programs

You are given a function f (x) = x3 + 2x2 + 3x + 1. Plot this functionusing matplotlib when x ∈ [−5, 5] with 512 points. Get the inputfrom the user to give a number x0 between [-2,2]. At the point(x0, f (x0), draw a tangent to this curve. Label the axes and curve.Save the output as a png file.

Suppose f (x , n) =∑n

k=1sin(2π(2k−1)x)

2k−1 . The use will give the value ofn. Plot f (x , n) for that n. See the trend as n increases.

Shivakumar,Bireswar (IIT Gandhinagar) Lecture 12: ES 102 Plotting With Matplotlib October 29, 2013 22 / 22