Lecture 13: ES 102 Plotting With Matplotlib Introduction to Computing IIT Gandhinagar, India Oct, 2013 logo Bireswar,Shivakumar (IIT Gandhinagar) Lecture 13 Oct, 2013 1 / 11 Simple Plotting Matplotlib provides the necessary functions for data visualization. pylab is an interface to matplotlib. Use import pylab as pl to use matplotlib. Matplotlib is generally used in conjunction with numpy. Use import numpy as np for that. This lecture is based on http://scipy-lectures.github.io/ intro/matplotlib/matplotlib.html. Visit the site for more information. 1 2 3 4 5 6 7 8 9 10 11 12 13 import numpy a s np import p y l a b a s p l x=np . a r a n g e ( − 4 , 4 , 0 . 1 ) s = np . s i n ( x ) # s i n d e f i n e d i n math and numpy # behaves d i f f e r e n t l y q = x∗x # e l e m e n t−w i s e m u l t i p l i c a t i o n pl . plot (x , s ) pl . plot (x , q) logo p l . show ( ) Bireswar,Shivakumar (IIT Gandhinagar) Lecture 13 Oct, 2013 2 / 11 Simple Plotting plot() generates an object that can be plotted. show() actually draws all the plottable objects created. logo Bireswar,Shivakumar (IIT Gandhinagar) Lecture 13 Oct, 2013 3 / 11 Customizing 1 2 3 4 5 6 7 8 9 10 11 12 13 import numpy a s np import p y l a b a s p l x=np . l i n s p a c e (−np . p i , np . p i , 2 0 0 , e n d p o i n t=True ) s = np . s i n ( x ) c = np . c o s ( x ) p l . 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 . 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 " ) p l . l e g e n d ( l o c=’ upper right ’ ) p l . show ( ) linspace(start,end,no of elms,endpoint=True/False): generate no of elms many equally spaced elements from start to end. end will not be included only if endpoint=False. color color of the curve;linewidth thickness of the curve;linestyle curve pattern; label set the name of the curve. legend where to show the names of the curves. logo Bireswar,Shivakumar (IIT Gandhinagar) Lecture 13 Oct, 2013 4 / 11 Customizing: Axes Limit, Ticks and Grid 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import numpy a s np import p y l a b a s p l x=np . l i n s p a c e (−2∗np . p i , 2 ∗ np . p i , 2 0 0 , e n d p o i n 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 i m ( x . min ( ) ∗ 1 . 1 , x . max ( ) ∗ 1 . 1 ) p l . ylim ( −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 , +1]) p l . g r i d ( True ) p l . show ( ) xlim sets x-axis limit, ylim sets y-axis limit. xticks,yticks puts the ticks in the axes. grid sets a rectangular grid. logo Bireswar,Shivakumar (IIT Gandhinagar) Lecture 13 Oct, 2013 5 / 11 Customizing: Changing Axes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import numpy a s np import p y l a b a s p l x=np . l i n s p a c e (−2∗np . p i , 2 ∗ np . p i , 2 0 0 , e n d p o i n t=True ) ax = p l . gc a ( ) # gca s t a n d s f o r ’ g e t c u r r e n t a x i s ’ ax . s p i n e s [ ’ right ’ ] . s e t c o l o r ( ’ none ’ ) # No c o l o r ax . s p i n e s [ ’ top ’ ] . s e t c o l o r ( ’ none ’ ) # No c o l o r ax . x a x i s . s e t t i c k s p o s i t i o n ( ’ bottom ’ ) # Where t o ax . s p i n e s [ ’ bottom ’ ] . s e t p o s i t i o n ( ( ’ data ’ , 0 ) ) # s e t ax . y a x i s . s e t t i c k s p o s i t i o n ( ’ left ’ ) # Where t o ax . s p i n e s [ ’ left ’ ] . s e t p o s i t i o n ( ( ’ data ’ , 0 ) ) # set put the put the the t i c k s bottom l i n e t o 0 i n data−s p a c e the t i c k s l e f t l i n e t o 0 i n data−s p a c e 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 . show ( ) logo Bireswar,Shivakumar (IIT Gandhinagar) Lecture 13 Oct, 2013 6 / 11 More Customizing 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import numpy a s np import p y l a b a s p l x=np . l i n s p a c e (−2∗np . p i , 2 ∗ np . p i , 2 0 0 , e n d p o i n 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 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 ( ) We can embed some LATEXcode in the program to get fancy fonts. logo Bireswar,Shivakumar (IIT Gandhinagar) Lecture 13 Oct, 2013 7 / 11 Bar Plot 1 2 3 4 5 6 7 8 9 10 11 import numpy a s np import p y l a b a s p l x=np . a r r a y ( [ 0 , 1 , 2 , 3 , 4 , 5 ] ) y=np . a r r a y ( [ 5 , 6 , 2 , 1 3 , 4 , 9 ] ) z=np . random . r a n d ( 6 ) ∗ 1 0 p l . b a r ( x , y , f a c e c o l o r=’ red ’ , e d g e c o l o r=’ white ’ ) p l . b a r ( x , −z , f a c e c o l o r=’ blue ’ , e d g e c o l o r=’ white ’ ) p l . show ( ) logo Bireswar,Shivakumar (IIT Gandhinagar) Lecture 13 Oct, 2013 8 / 11 Scatter Plot 1 2 3 4 5 6 7 8 9 10 11 import numpy a s np import p y l a b a s p l r 1=np . random . r a n d ( 1 0 0 0 ) x=( r1 −0.5)∗10 r 2=np . random . r a n d ( 1 0 0 0 ) y=10∗ r 2 pl . scatter (x , y) p l . show ( ) logo Bireswar,Shivakumar (IIT Gandhinagar) Lecture 13 Oct, 2013 9 / 11 Polar Plot 1 2 3 4 5 6 7 8 9 10 11 12 13 import p y l a b a s p l import numpy a s np ax = p l . a x e s ( p o l a r=True ) # Only c h a n g e HERE N = 200 t h e t a = np . a r a n g e ( 0 . 0 , 2 ∗ np . p i , 2 ∗ np . p i / N) r a d i i = 10 ∗ np . s i n ( t h e t a ) p l . p l o t ( t h e t a , r a d i i , c o l o r=’ red ’ ) p l . p l o t ( theta ,5∗ t h e t a ) p l . show ( ) logo Bireswar,Shivakumar (IIT Gandhinagar) Lecture 13 Oct, 2013 10 / 11 Plotting Data from File In many situations the data to be plotted is in a file. You can read the data, store it in arrays and plot the data. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import numpy a s np import p y l a b a s p l d a t a f i l e = open ( " data . txt " , " r " ) x =[] y =[] for l i n e in d a t a f i l e : temp= l i n e . s p l i t ( ) p= f l o a t ( temp [ 0 ] ) q= f l o a t ( temp [ 1 ] ) x+=[p ] y+=[q ] X=np . a r r a y ( x ) Y=np . a r r a y ( y ) p l . p l o t (X , Y , 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 . show ( ) logo Bireswar,Shivakumar (IIT Gandhinagar) Lecture 13 Oct, 2013 11 / 11
© Copyright 2026 Paperzz