Mat Plot Lib
Mat Plot Lib
[4]: df=pd.read_csv('./iris.csv')
[5]: df
[6]: x=np.arange(0,10)
y= x**2
y2=2*x+3
keshav kolekar
[7]: plt.plot(x,y)
plt.plot(x,y2)
plt.show()
[8]: themes=plt.style.available
print(themes)
[9]: plt.style.use('classic')
[10]: plt.plot(x,y,color='red',marker='o')
plt.plot(x,y2,color='green',marker='*')
plt.xlabel("time")
keshav kolekar
plt.ylabel("price")
# plt.xlim(0,12)
plt.title("Prices of smartphone over time")
plt.legend()
plt.show()
C:\Users\Keshav\AppData\Local\Temp\ipykernel_8252\2900917789.py:8: UserWarning:
No artists with labels found to put in legend. Note that artists whose label
start with an underscore are ignored when legend() is called with no argument.
plt.legend()
[ ]:
2 Scatter plot
plt.scatter()
scatters data points across x and y axis
keshav kolekar
[12]: plt.figure(figsize=(10,6))
plt.scatter(x,y,color='red',label='iphone')
plt.scatter(x,y2,color='green',label='android')
plt.xlabel("time")
plt.xlabel("price")
plt.show()
[13]: data=np.random.randn(100,2)
[14]: plt.scatter(x=data[:,0],y=data[:,1])
plt.show()
keshav kolekar
3 Bar Graphs
plt.bar()
plot a bar graph between heights and labels you provide
[16]: x=np.arange(0,5)
car1=[40,20,34,45,12]
car2=[20,40,24,55,62]
plt.bar(x+0.3,car2,width=0.3,label='car2')
plt.xlabel("years")
plt.ylabel("sales in doller")
plt.title("car price comparision")
plt.legend()
plt.xlim(-1,5)
plt.ylim(0,90)
keshav kolekar
plt.show()
# plt.show()
4 Pie chart
plt.pie()
plot the pie chart with the provide labels and value
[19]: # plt.pie?
[20]: subjects=['physics','maths','chemistry','english']
marks=np.array([45,97,67,78])
[21]: plt.pie(x=marks,labels=subjects,explode=[0,0.100,0,0],shadow=True,autopct="%0.
↪0f")
plt.show()
keshav kolekar
5 Histograms
plt.hist()
creating a dummy data using normal distribution
[24]: maths_mu=70
maths_sig=8
maths_marks= np.round(x_std*maths_sig + maths_mu)
phy_mu=50
phy_sig=10
phy_mu=np.round(x_std*phy_sig + phy_mu)
keshav kolekar
[25]: maths_marks
[25]: array([64., 54., 65., 61., 61., 67., 68., 60., 78., 79., 77., 63., 72.,
71., 70., 68., 83., 86., 72., 73., 66., 74., 77., 72., 69., 57.,
67., 74., 70., 83., 76., 77., 69., 79., 67., 54., 57., 67., 74.,
83., 55., 76., 61., 79., 86., 68., 75., 69., 69., 70., 71., 70.,
63., 73., 76., 69., 80., 79., 70., 64., 59., 72., 62., 64., 75.,
62., 76., 67., 79., 65., 72., 73., 79., 68., 73., 74., 57., 56.,
73., 77., 59., 88., 76., 58., 76., 56., 71., 54., 70., 69., 79.,
70., 74., 86., 59., 69., 82., 84., 82., 77., 55., 68., 56., 73.,
74., 78., 82., 70., 70., 80., 66., 68., 66., 74., 66., 46., 72.,
77., 67., 77., 67., 60., 78., 71., 59., 78., 73., 81., 73., 51.,
84., 60., 70., 62., 57., 54., 68., 52., 70., 77., 78., 74., 69.,
65., 76., 52., 65., 77., 68., 71., 59., 64., 69., 56., 74., 61.,
74., 73., 69., 64., 73., 72., 67., 64., 68., 86., 93., 72., 68.,
66., 64., 81., 60., 61., 66., 69., 71., 68., 80., 77., 70., 69.,
80., 71., 57., 65., 76., 68., 70., 69., 75., 83., 72., 62., 66.,
65., 65., 67., 72., 77., 97., 79., 67., 65., 77., 64., 77., 69.,
79., 85., 82., 61., 61., 60., 58., 78., 66., 69., 62., 63., 82.,
72., 79., 70., 75., 73., 70., 67., 90., 74., 71., 77., 59., 70.,
81., 73., 67., 67., 73., 64., 61., 75., 85., 66., 67., 75., 84.,
75., 77., 67., 71., 66., 81., 73., 60., 79., 75., 67., 78., 78.,
62., 77., 64., 70., 69., 79., 76., 82., 66., 67., 66., 56., 64.,
63., 74., 80., 73., 56., 57., 63., 72., 75., 65., 61., 65., 70.,
74., 65., 70., 74., 80., 70., 58., 72., 76., 78., 61., 82., 73.,
67., 71., 57., 69., 81., 66., 47., 72., 56., 67., 69., 72., 67.,
65., 72., 75., 70., 64., 75., 75., 81., 59., 76., 66., 65., 65.,
69., 80., 80., 62., 71., 60., 77., 76., 72., 67., 70., 78., 74.,
80., 75., 82., 73., 69., 66., 71., 52., 53., 59., 81., 74., 65.,
66., 86., 62., 60., 81., 72., 66., 64., 76., 72., 72., 88., 70.,
93., 74., 71., 73., 76., 69., 69., 75., 72., 86., 67., 70., 78.,
79., 74., 78., 70., 79., 80., 72., 64., 74., 58., 65., 77., 60.,
62., 60., 68., 70., 58., 61., 77., 42., 64., 68., 57., 69., 81.,
56., 76., 64., 74., 59., 74., 64., 59., 66., 76., 75., 65., 67.,
81., 76., 60., 65., 75., 71., 62., 69., 79., 66., 77., 92., 81.,
71., 71., 70., 60., 70., 80., 67., 70., 69., 65., 75., 80., 66.,
65., 73., 77., 59., 76., 66., 65., 75., 64., 67., 81., 56., 66.,
74., 79., 80., 75., 78., 71., 57., 78., 68., 67., 83., 66., 69.,
70., 75., 70., 63., 74., 78., 75., 65., 63., 53., 87., 75., 67.,
70., 89., 64., 76., 48., 71., 71., 70., 61., 69., 75., 66., 64.,
71., 70., 52., 80., 73., 78.])
[26]: plt.hist(maths_marks,bins=10)
# plt.xlim(0,150)
plt.hist(phy_mu,bins=10,alpha=0.6) # alpha is for opacity
plt.show()
keshav kolekar
[27]: maths_marks.min()
[27]: 42.0
[28]: maths_marks.max()
[28]: 97.0
[29]: maths_marks.mean()
[29]: 70.182
[31]: x=np.arange(0,10,0.1)
y1=x**2
y2=x**3
y3=np.sin(x)
keshav kolekar
y4=np.cos(x)
[32]: plt.subplot(2,2,1) # (2,2,1) this represent 2 rows and 2 column and 1 quadrant
plt.plot(x,y1)
plt.subplot(2,2,2)
plt.plot(x,y2)
plt.subplot(2,2,3)
plt.plot(x,y3)
plt.subplot(2,2,4)
plt.plot(x,y4)
plt.show()
10
keshav kolekar
plt.subplot(2,3,2)
plt.plot(x,y2)
plt.subplot(2,3,3)
plt.plot(x,y3)
plt.subplot(2,3,4)
plt.plot(x,y4)
plt.subplot(2,3,5)
plt.plot(x,y4)
plt.subplot(2,3,6)
plt.plot(x,y4)
plt.show()
11
keshav kolekar
7 3D plots
[35]: a=np.arange(4)
b=np.arange(4,7)
[36]: b
[38]: a
[39]: b
[40]: a+b
[41]: # fig=plt.figure()
# ax = fig.gca(projection='3d')
# ax.plot_surface(a,b,a+b)
# plt.show()
fig = plt.figure()
# Add a 3D subplot
ax = fig.add_subplot(111, projection='3d')
12
keshav kolekar
[42]: import matplotlib.pyplot as plt
import numpy as np
# Create a figure
fig = plt.figure()
# Add a 3D subplot
ax = fig.add_subplot(111, projection='3d')
13
keshav kolekar
# Plot the surface
ax.plot_surface(a, b, z, cmap='viridis')
[44]: img=plt.imread('./pikachu.jpg')
14
keshav kolekar
[45]: type(img)
[45]: numpy.ndarray
[46]: plt.imshow(img )
# plt.axis('off')
plt.show()
15
keshav kolekar
[48]: array([[[145, 179, 225],
[144, 178, 223],
[144, 178, 223],
…,
[ 74, 114, 183],
[ 74, 114, 183],
[ 74, 114, 183]],
…,
[[ 0, 65, 31],
[ 0, 69, 35],
[ 1, 72, 38],
…,
[ 1, 71, 37],
[ 0, 70, 36],
[ 0, 69, 35]],
[[ 6, 73, 40],
[ 4, 74, 40],
[ 2, 72, 38],
…,
[ 1, 71, 37],
[ 0, 70, 36],
[ 0, 69, 35]],
[[ 3, 70, 37],
[ 0, 69, 35],
[ 0, 70, 36],
…,
[ 0, 69, 35],
16
keshav kolekar
[ 0, 69, 35],
[ 0, 69, 35]]], dtype=uint8)
[49]: pikachu_face=img[330:515,190:390,:]
[50]: pikachu_face.shape
[51]: plt.imshow(pikachu_face)
plt.axis('off')
plt.show()
[53]: img_copy=img.copy()
17
keshav kolekar
[54]: img_copy[:,:,0]=0
[55]: plt.imshow(img_copy)
plt.axis('off')
plt.show() # there is no read color inthe img
[56]: img_copy1=img.copy()
img_copy1[:,:,1]=0
plt.imshow(img_copy1)
plt.axis('off')
plt.show()
18
keshav kolekar
[57]: img_copy2=img.copy()
img_copy2[:,:,2]=0
plt.imshow(img_copy2)
plt.axis('off')
plt.show()
19
keshav kolekar
[58]: # border image solution
row=np.zeros((10,400,3),dtype='int')
[59]: row.shape
[60]: plt.imshow(row)
plt.axis('off')
plt.show()
[61]: mod_img=np.vstack((row,img,row))
20
keshav kolekar
[62]: plt.imshow(mod_img)
plt.axis('off')
plt.show()
[63]: columns=np.zeros((600,10,3),dtype='int')
[64]: columns.shape
[65]: plt.imshow(columns)
plt.axis('off')
plt.show()
21
keshav kolekar
[66]: mod_img=np.hstack((columns,mod_img,columns))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[66], line 1
----> 1 mod_img=np.hstack((columns,mod_img,columns))
ValueError: all the input array dimensions except for the concatenation axis␣
↪must match exactly, but along dimension 0, the array at index 0 has size 600␣
22
keshav kolekar
[ ]: plt.imshow(mod_img)
plt.axis('off')
plt.show()
[69]: iris=pd.read_csv('./iris.csv')
[71]: iris.head()
[73]: petal_length=iris['petal_length']
petal_width=iris['petal_width']
[75]: plt.scatter(x=petal_length,y=petal_width)
plt.xlabel('petal_length')
plt.ylabel('petal_width')
plt.show()
23
keshav kolekar
[77]: setosa=iris[iris['species']=='setosa']
virginica=iris[iris['species']=='virginica']
versicolor=iris[iris['species']=='versicolor']
[79]: setosa
24
keshav kolekar
13 4.3 3.0 1.1 0.1 setosa
14 5.8 4.0 1.2 0.2 setosa
15 5.7 4.4 1.5 0.4 setosa
16 5.4 3.9 1.3 0.4 setosa
17 5.1 3.5 1.4 0.3 setosa
18 5.7 3.8 1.7 0.3 setosa
19 5.1 3.8 1.5 0.3 setosa
20 5.4 3.4 1.7 0.2 setosa
21 5.1 3.7 1.5 0.4 setosa
22 4.6 3.6 1.0 0.2 setosa
23 5.1 3.3 1.7 0.5 setosa
24 4.8 3.4 1.9 0.2 setosa
25 5.0 3.0 1.6 0.2 setosa
26 5.0 3.4 1.6 0.4 setosa
27 5.2 3.5 1.5 0.2 setosa
28 5.2 3.4 1.4 0.2 setosa
29 4.7 3.2 1.6 0.2 setosa
30 4.8 3.1 1.6 0.2 setosa
31 5.4 3.4 1.5 0.4 setosa
32 5.2 4.1 1.5 0.1 setosa
33 5.5 4.2 1.4 0.2 setosa
34 4.9 3.1 1.5 0.1 setosa
35 5.0 3.2 1.2 0.2 setosa
36 5.5 3.5 1.3 0.2 setosa
37 4.9 3.1 1.5 0.1 setosa
38 4.4 3.0 1.3 0.2 setosa
39 5.1 3.4 1.5 0.2 setosa
40 5.0 3.5 1.3 0.3 setosa
41 4.5 2.3 1.3 0.3 setosa
42 4.4 3.2 1.3 0.2 setosa
43 5.0 3.5 1.6 0.6 setosa
44 5.1 3.8 1.9 0.4 setosa
45 4.8 3.0 1.4 0.3 setosa
46 5.1 3.8 1.6 0.2 setosa
47 4.6 3.2 1.4 0.2 setosa
48 5.3 3.7 1.5 0.2 setosa
49 5.0 3.3 1.4 0.2 setosa
[81]: plt.
↪scatter(setosa['petal_length'],setosa['petal_width'],color='green',label="setosa")
plt.
↪scatter(virginica['petal_length'],virginica['petal_width'],color='red',label='virginica')
plt.
↪scatter(versicolor['petal_length'],versicolor['petal_width'],color='blue',label='versicolor'
plt.xlabel('petal_length')
plt.ylabel('petal_width')
plt.xlim(0,12)
25
keshav kolekar
plt.legend()
plt.show()
[83]: plt.hist(iris['petal_length'],bins=20)
plt.xlabel('petal_length')
plt.ylabel('petal_width')
plt.show()
26
keshav kolekar
[85]: iris['species'].value_counts()
[85]: species
setosa 50
versicolor 50
virginica 50
Name: count, dtype: int64
[87]: plt.pie(iris['species'].value_counts(),labels=iris['species'].value_counts().
↪index,autopct='%0.0f')
plt.show()
27
keshav kolekar
[89]: movie_meta=pd.read_csv('./movie_meta.csv')
[91]: movie_meta.shape
[93]: movie_meta.describe()
28
keshav kolekar
actor_3_facebook_likes actor_1_facebook_likes gross \
count 5020.000000 5036.000000 4.159000e+03
mean 645.009761 6560.047061 4.846841e+07
std 1665.041728 15020.759120 6.845299e+07
min 0.000000 0.000000 1.620000e+02
25% 133.000000 614.000000 5.340988e+06
50% 371.500000 988.000000 2.551750e+07
75% 636.000000 11000.000000 6.230944e+07
max 23000.000000 640000.000000 7.605058e+08
[95]: movie_meta.columns
29
keshav kolekar
'actor_3_name', 'facenumber_in_poster', 'plot_keywords',
'movie_imdb_link', 'num_user_for_reviews', 'language', 'country',
'content_rating', 'budget', 'title_year', 'actor_2_facebook_likes',
'imdb_score', 'aspect_ratio', 'movie_facebook_likes'],
dtype='object')
[97]: movie_meta.head()
movie_facebook_likes
0 33000
1 0
2 85000
30
keshav kolekar
3 164000
4 0
[5 rows x 28 columns]
[99]: movie_meta.tail()
movie_facebook_likes
5038 84
5039 32000
5040 16
5041 660
31
keshav kolekar
5042 456
[5 rows x 28 columns]
[ ]:
[ ]:
32
keshav kolekar