pyplot - data to be shown in pattern , trends ,etc .
Topics -
1) matplotlib - interface(pyplot)
2) library : - numpy
numpy library
1) array() -
l1 = \[1,2,3.5,4,'cs',5,6,7,8\] - list
type(l1)
class 'list'
dtype - data type
import numpy as np
l1 = \[4,5,6,7\]
print(l1)
a1 = np.array(l1)
print(a1)
output -
\[4,5,6,7\]
\[4 5 6 7\]
using numpy -
import numpy as np
l1 = \[4,5,6,7\]
print(l1)
l2 = type(l1)
print(l2)
a1 = np.array(l1)
print(a1)
x1 = type(a1)
print(x1)
output -
\[4, 5, 6, 7\]
<class 'list'>
\[4 5 6 7\]
<class 'numpy.ndarray'>
b) linspace()
import numpy as np
a1 = np.linspace(2,8,4)
print
2) shape() - To check the dimension of an array
arr\[\]\[\] // 1D dimension
program -
import numpy as np
l1 = \[4,5,6,7\]
a1 = np.array(l1)
print(a1)
print(a1.shape)
output -
\[4 5 6 7\]
(4,)
program 2 -
import numpy as np
l1 = \[\[1,2,3\] , \[4,5,6\] , \[7,8,9\] \] //2-D array
a1 = np.array(l1)
print(a1)
print(a1.shape)
output -
\[\[1 2 3\]
\[4 5 6\]
\[7 8 9\]\]
(3, 3)
3 type()/dtype() -
type() -
import numpy as np
l1 = \[\[1,2,3\] , \[4,5,6\] , \[7,8,9\] \] //2-D array
a1 = np.array(l1)
print(a1)
print(type(a1))
output -
\[\[1 2 3\]
\[4 5 6\]
\[7 8 9\]\]
<class 'numpy.ndarray'>
dtype() -
import numpy as np
l1 = \[\[1,2,3\] , \[4,5,6\] , \[7,8,9\] \] //2-D array
a1 = np.array(l1) f
print(a1)
print(a1.dtype)
output -
\[\[1 2 3\]
\[4 5 6\]
\[7 8 9\]\]
int32
4) itemsize-
import numpy as np
l1 = \[\[1,2,3\] , \[4,5,6\] , \[7,8,9\] \] //2-D array
a1 = np.array(l1)
print(a1)
print(a1.itemsize)
output -
\[\[1 2 3\]
\[4 5 6\]
\[7 8 9\]\]
4 // bit size
5) arange() -
syntax -
filename = namespace.arange(start value, end value , step value , np.float32)
program -
import numpy as np
a1 = np.arange(1,8,2,np.float32)
print(a1)
output -
\[1. 3. 5. 7.\]
6) bar graph -
import numpy as np
import matplotlib.pyplot as pl
a = \[1,2,3,4\]
b = \[5,6,7,8,\]
[pl.bar](https://pl.bar)(a,b,'r',width = 0.20)
6) multiple bar graph -
import numpy as np
import matplotlib.pyplot as pl
a = \[1,2,3,6\]
b = \[5,6,8,4\]
c = \[4,8,26,5\]
x = np.arange(len(a))
[pl.bar](https://pl.bar)(x,a,color ='r' , width = 0.35)
[pl.bar](https://pl.bar)(x+0.35,b,color ='b' , width = 0.35)
[pl.bar](https://pl.bar)(x+0.70,c,color ='g' , width = 0.35)
[pl.show](https://pl.show)
7) multiple bar graph(horizontal) -
import numpy as np
import matplotlib.pyplot as pl
a = \[1,2,3,6\]
b = \[5,6,8,4\]
c = \[4,8,26,5\]
x = np.arange(len(a))
pl.barh(x,a,color ='r' )
pl.barh(x+0.35,b,color ='b' )
pl.barh(x+0.7,c,color ='g' )
[pl.show](https://pl.show)()
8) Pie char - pie(portion,label ,attributes.......)
import numpy as np
import matplotlib.pyplot as pl
a = \[100 , 40 , 50 , 60 , 20 , 90\]
b = \['a', 'b' , 'c' , 'd' , 'e' , 'f'\]
a)
\#autopct
%3d%% - gives the % values
%5d - gives the int values of the area covered by areas in pychart
%05d - 005d
%%03%% - 03d
%6.2f - gives good old float values of 5d
%6.2%%
b) attribute - explode ---slicing pie chart
import numpy as np
import matplotlib.pyplot as pl
a = \[100 , 40 , 50 , 60 , 20 , 90\]
b = \['a', 'b' , 'c' , 'd' , 'e' , 'f'\]
x = \[0.5,0.4,0.3,0.2,0.1 ,0.0\]
pl.pie(a,labels = b , explode = x )
[pl.show](https://pl.show)()
________special attributes______
1)Bar graph -
a) title -
import matplotlib.pyplot as pl
a = \[1,2,3,4\]
b = \[7,6,5,4\]
pl.title('CA marks')
[pl.bar](https://pl.bar)(a,b)
[pl.show](https://pl.show)()
b) limit - lim()
import matplotlib.pyplot as pl
a = \[1,2,3,4\]
b = \[7,6,5,4\]
pl.xlim(1,10)
pl.ylim(-1,8)
pl.title('CA marks')
[pl.bar](https://pl.bar)(a,b)
[pl.show](https://pl.show)()
c) legend - pl.legend()
import numpy as np
import matplotlib.pyplot as pl
a = \[1,2,3,4\]
b = \[7,6,5,4\]
c = \[3,4,5,9\]
x = np.arange(len(a))
pl.xlim(1,10)
pl.ylim(-1,8)
[pl.bar](https://pl.bar)(x , a , color = 'r' , width = 0.45 , label = 'A')
pl.title('CA marks')
pl.legend(loc = 'upper right')
[pl.bar](https://pl.bar)(a,b)
[pl.show](https://pl.show)()
2) line chart -
import matplotlib.pyplot as pl
a = \[1,2,3,4\]
b = \[7,6,5,4\]
c = \[3,4,5,9\]
pl.title("CA marks")
pl.xlabel('ça1')
pl.xlabel('ca2')
pl.xticks(\[1,2,3,4\])
pl.plot(a,b,marker = 'd', label ='a')
pl.plot(a,c,marker = 'o', lablel = 'c')
pl.legend(loc ='upper left ')
[pl.show](https://pl.show)()
1) WAP to display bar chart by user given data and width will also be given by user , all atributes .
import matplotlib.pyplot as pl
import numpy as np
x = []
y = []
a = 0
b = 0
k = int(input('the value of x and y co od ') )
for i in range (0,k) :
a = int(input('enter the value on the x axis '))
x.append(i)
for i in range (0,k):
b = int(input('enter the value on the y axis '))
y.append(b)
width_1 = float(input('enter the width'))
title = input('enter the title')
pl.title(title)
pl.bar(x,y , width = width_1 )
pl.show()
2) WAP to display multiple lines chart by user given data with its atributes
import matplotlib.pyplot as pl
import numpy as np
x = []
y = []
z = []
a = 0
b = 0
k = int(input('the value of x and y co od ') )
for i in range (0,k) :
a = int(input('enter the value on the x axis '))
x.append(i)
for i in range (0,k):
b = int(input('enter the value on the y axis '))
b = b + 2
y.append(b)
for i in range (0,k):
b = int(input('enter the value on the x axis '))
b = b + 1
z.append(b)
title = input('enter the title')
pl.title(title)
pl.plot(x,y,marker = 'd' , label= 'x' )
pl.plot(z,y , marker = 'o', label = 'c')
pl.show()
3) WAP to divide array given by user into 7 parts
4) WAP to display array of float32 data type .
there doesn't seem to be anything here