
Introduction to Python
Dr. Cem Özdo
˘
gan
LOGIK
Introduction to Python
Python Libraries for
Data Science
NumPy
SciPy
Matplotlib
Programming
Examples in Python
2.27
Programming in Python XIV
1 # imp ortin g a l l f unction s from pylab module
2 from pylab i mport
*
# Not the p refer r e d methodology . Means t o
bring e veryt hin g i n to the top l e v e l name space
3 x = arange (1 , 10 , 0 . 5 ) ; p r i n t ( x )
4 xsquare = x
* *
2 ; p r i n t ( xsquare )
5 xcube = x
* *
3 ; p r i n t ( xcube )
6 xsquareroo t = x
* *
0 . 5 ; p r i n t ( xsquareroot )
7 fi g u r e (1 ) # open f i g u r e 1
8 pl o t ( x , xsquare ) # basic plo t
9 x l a bel ( ’ abscissa ’ ) # add a l a b e l t o the x axi s
10 ylab e l ( ’ ordina t e ’ ) # add a l a b e l to the y a xis
11 t i t l e ( ’A prac t i c e p l o t ’ ) # add a t i t l e
12 savefig ( ’ pl ot_1 . png ’ ) # save the f i g u r e to the c u r rent d i r e t o r y
13 f i g u r e ( 2) # open a second f i g u r e
14 p l o t ( x , xsquare , ’ ro ’ ,x , xcube , ’g+−− ’ ) # Two p l o t s . Red c i r c l e s wi th
no l i n e . Green plus signs j o i ned by a dashed curve
15 xlab e l ( ’ abscissa ’ ) # x and y lab els , t i t l e
16 ylab e l ( ’ ordina t e ’ ) # x and y label s , t i t l e
17 t i t l e ( ’More p racti c e ’ ) # x and y label s , t i t l e
18 legend ( ( ’ squared ’ , ’ cubed ’ ) ) # add a legend
19 savefig ( ’ pl ot_2 . png ’ ) # save the f i g u r e
20 f i g u r e ( 3) # open a t h i r d f i g u r e
21 sub plot (3 , 1 ,1) ; p l o t ( x , xsquareroot , ’ k
*
: ’ ) # Black s t ars+do tted l i n e
22 t i t l e ( ’ square r o o ts ’ ) # add a t i t l e
23 sub plot (3 , 1 ,2) ; p l o t ( x , xsquare , ’ r>− ’ ) # Red t r i a n g l e s +dashed l i n e
24 t i t l e ( ’ squares ’ ) # add a t i t l e
25 sub plot (3 , 1 ,3) ; p l o t ( x , xcube , ’mh− ’ ) # Magenta hexagons+ s o l i d l i n e
26 t i t l e ( ’ cubes ’ ) # add a t i t l e
27 savefig ( ’ pl ot_3 . png ’ ) # save the f i g u r e
28 show ( )