Matplotlib
In [1]: import numpy as np
import pandas as pd
import matplotlib
from matplotlib import pyplot as plt
/Users/satyakumarpalli/anaconda3/lib/python3.10/site-packages/pandas/core/arrays/[Link]: UserWarning: Pandas requires version '1.3.6' or newer of 'bottleneck' (ve
rsion '1.3.5' currently installed).
from [Link] import (
Line Plot
In [2]: a = [Link]([1,2,3,4,5,6,7,8])
In [3]: [Link](a)
Out[3]: [<[Link].Line2D at 0x14acb3dc0>]
In [4]: a = [Link]([1,2,3,4])
b = [Link]([10,20,30,40])
[Link](a,b)
Out[4]: [<[Link].Line2D at 0x14adeadd0>]
In [5]: a = [Link]([1,2,3,4])
b = [Link]([10,20,30,40])
[Link](a,b,'r')
Out[5]: [<[Link].Line2D at 0x14af7d240>]
'r'. - red 'g'. - green 'b'. - blue 'c'. - cyan 'm'. - megenta 'y'. - yellow 'w'. - white 'k'. - black
In [6]: a = [Link]([1,2,3,4])
b = [Link]([10,20,30,40])
[Link](a,b,'red')
Out[6]: [<[Link].Line2D at 0x14afeb7c0>]
In [7]: a = [Link]([1,2,3,4])
b = [Link]([10,20,30,40])
[Link](a,b,'green')
Out[7]: [<[Link].Line2D at 0x14b072110>]
In [8]: x = [Link](1,100,1000)
y = [Link](x)
[Link](x,y,'m')
[Link]()
In [9]: x = [Link](1,100,1000)
y = [Link](x)
[Link](x,y,'m')
[Link]('Time')
[Link]('Amplitude')
[Link]()
In [10]: [Link](figsize=(8,3)) #size = width,hight
x = [Link](1,100,1000)
y = [Link](x)
[Link](x,y,'m')
[Link]('Time')
[Link]('Amplitude')
[Link]()
In [11]: [Link](figsize=(8,3)) #size = width,hight
x = [Link](1,100,1000)
y = [Link](x)
[Link](x,y,'g')
[Link]('Time')
[Link]('Amplitude')
[Link]()
[Link]()
In [12]: [Link](figsize=(8,3)) #size = width,hight
x = [Link](1,100,1000)
y = [Link](x)
[Link](x,y,'g')
[Link]('Time')
[Link]('Amplitude')
[Link]('Sinewave')
[Link]()
[Link]()
In [13]: [Link](figsize=(4,3))
a = [Link]([10,45,23,67,12])
[Link](a,'b')
[Link]()
In [14]: [Link](figsize=(4,3))
a = [Link]([10,45,23,67,12])
[Link](a,'r:*')
[Link]()
In [15]: [Link](figsize=(8,3)) #size = width,hight
x = [Link](1,100,1000)
y = [Link](x)
[Link](x,y,'b:')
[Link]('Time')
[Link]('Amplitude')
[Link]('Sinewave')
[Link]()
[Link]()
Line Reference 1. '-' solid line 2. ':' Dotted line 3. '--' dashed line 4. '-.' dash dot line
Marker reference 'o' circle '*' star '.' point 'x' X 'X' X(filled) '+' Plus 'P' Plus(filled) 's' Square 'D' Dimond 'v' Traiangle down '^' traingle up '<' traingle left '>' traingle right
In [16]: [Link](figsize=(4,3))
a = [Link]([10,45,23,67,12])
[Link](a,'r:*')
[Link]()
In [17]: [Link](figsize=(4,3))
a = [Link]([10,45,23,67,12])
[Link](a,'b-.P')
[Link]()
In [18]: [Link](figsize=(4,3))
a = [Link]([10,45,23,67,12])
[Link](a,marker='D',mfc='r',mec='b')
[Link]()
In [19]: [Link](figsize=(4,3))
a = [Link]([10,45,23,67,12])
[Link](a,marker='D',ms=10,mfc='r',mec='b')
[Link]()
In [20]: [Link](figsize=(4,3))
a = [Link]([10,45,23,67,12])
[Link](a,color='y',marker='D',ms=10,mfc='r',mec='k')
[Link]()
In [21]: #plot multiple line plots in single figure window
In [22]: [Link](figsize=(8,4)) #size = width,hight
x = [Link](0,10,1000)
y = [Link](x)
z = [Link](x)
[Link](x,y,'g-',label='sinewave')
[Link](x,z,'r-',label='cosine wave')
[Link]('Time')
[Link]('Amplitude')
[Link]('Sinewave')
[Link]()
[Link](loc='upper right')
[Link]()
subplots
In [23]: [Link](figsize=(6,3))
x = [Link](0,10,1000)
y = [Link](x)
[Link](1,2,1)
[Link](x,y,'r')
[Link]()
z = [Link](x)
[Link](1,2,2)
[Link](x,z,'g')
[Link]()
In [24]: [Link](figsize=(6,3))
x = [Link](0,10,1000)
y = [Link](x)
[Link](2,2,1)
[Link](x,y,'r')
[Link]()
z = [Link](x)
[Link](2,2,2)
[Link](x,z,'g')
[Link]()
[Link](2,2,3)
[Link](x-20,y,'y')
[Link]()
[Link](2,2,4)
[Link](x-40,y,'m')
[Link]()
In [25]: [Link](figsize=(6,3)) #size = width,hight
x = [Link](0,10,1000)
y = [Link](x)
[Link](x,y,color='#76d7c4',linewidth=4)
[Link]('Time')
[Link]('Amplitude')
[Link]('Sinewave')
[Link]()
[Link]()
Bar plot
In [ ]:
In [26]: a =['satya','sekhar','jayanth','Vishnu']
b =[25,50,75,90]
[Link](figsize=(6,3)) #size = width,hight
[Link](a,b)
[Link]()
In [27]: a =['satya','sekhar','jayanth','Vishnu']
b =[25,50,75,90]
[Link](figsize=(6,3)) #size = width,hight
[Link](a,b,color='m')
[Link]()
In [28]: a =['satya','sekhar','jayanth','Vishnu']
b =[25,50,75,90]
[Link](figsize=(6,3)) #size = width,hight
[Link](a,b,color='m',width=0.4)
[Link]()
In [29]: a =['satya','sekhar','jayanth','Vishnu']
b =[25,50,75,90]
[Link](figsize=(6,3)) #size = width,hight
[Link](a,b)
[Link]()
In [30]: df = pd.read_csv('cleaned_titanic.csv')
In [31]: [Link]('Unnamed: 0',axis='columns',inplace=True)
df
Out[31]: survived pclass sex age sibsp parch fare embarked class who adult_male embark_town alive alone
0 0 3 male 22.000000 1 0 7.2500 S Third man True Southampton no False
1 1 1 female 38.000000 1 0 71.2833 C First woman False Cherbourg yes False
2 1 3 female 26.000000 0 0 7.9250 S Third woman False Southampton yes True
3 1 1 female 35.000000 1 0 53.1000 S First woman False Southampton yes False
4 0 3 male 35.000000 0 0 8.0500 S Third man True Southampton no True
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
886 0 2 male 27.000000 0 0 13.0000 S Second man True Southampton no True
887 1 1 female 19.000000 0 0 30.0000 S First woman False Southampton yes True
888 0 3 female 29.699118 1 2 23.4500 S Third woman False Southampton no False
889 1 1 male 26.000000 0 0 30.0000 C First man True Cherbourg yes True
890 0 3 male 32.000000 0 0 7.7500 Q Third man True Queenstown no True
891 rows × 14 columns
In [41]: [Link](figsize=(4,2))
df['sex'].value_counts().plot(kind='bar',color = 'r',width=0.2)
Out[41]: <Axes: xlabel='sex'>
In [42]: #plot a bar plot to show how many mem, women and child passengers were onboarded into ship?
In [44]: [Link](figsize=(4,2))
df['who'].value_counts().plot(kind='bar',color = 'm',width=0.2)
Out[44]: <Axes: xlabel='who'>
In [45]: #plot a bar graph to show how many females are survived and not survived
In [54]: df[df['sex']=='female'].groupby('survived')['sex'].count().plot(kind='bar',color='y')
Out[54]: <Axes: xlabel='survived'>
In [57]: df[df['sex']=='female'].groupby('survived')['sex'].count().plot(kind='bar',color='c')
Out[57]: <Axes: xlabel='survived'>
In [58]: #plot a bar graph to show how many passengers from third class were alive and how many were
#not alive
In [60]: df[df['class']=='Third'].groupby('alive')['class'].count().plot(kind='bar',color='m')
Out[60]: <Axes: xlabel='alive'>
In [61]: #plot a bar graph to show how many adultmale passengers embark town was southampton and
# and queenstown
In [65]: df[df['embark_town'].isin(['Southampton','Queenstown'])].groupby('embark_town')['adult_male'].count()
Out[65]: embark_town
Queenstown 77
Southampton 646
Name: adult_male, dtype: int64
In [81]: [Link](figsize=(4,2))
df[df['adult_male']==True].groupby('embark_town')['adult_male'].count()[1:].plot(kind='bar')
Out[81]: <Axes: xlabel='embark_town'>
In [ ]: df[df['adult_male']==True]
In [76]: df[df['adult_male']==True][(df['embark_town']=='Southampton')|(df['embark_town']=='Queenstown')]['embark_town'].value_counts().plot(kind='bar')
/var/folders/nn/9bkkh7612wg2c3gglt0zd9h40000gn/T/ipykernel_81114/[Link]: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
df[df['adult_male']==True][(df['embark_town']=='Southampton')|(df['embark_town']=='Queenstown')]['embark_town'].value_counts().plot(kind='bar')
Out[76]: <Axes: xlabel='embark_town'>
sctter plot
In [82]: #[Link]()
In [85]: [Link](figsize=(4,4))
a = [Link]([34,56,78,90,12,34,52])
b = [Link]([45,67,89,23,90,45,12])
[Link](a,b)
[Link]()
In [86]: a = [Link](1,100,30)
In [89]: b = [Link](10,100,30)
[Link](a,b,color = 'hotpink')
[Link]()
In [90]: a = [Link](1,100,30)
b = [Link](10,100,30)
c = [Link](1,100,20)
d = [Link](10,100,20)
[Link](a,b,color = 'hotpink')
[Link](c,d,color='yellowgreen')
[Link]()
In [117… a = [Link](1,100,10)
b = [Link](10,100,10)
cols = ['red','green','blue','cyan','magenta','yellow','hotpink','yellowgreen','brown','red']
[Link](a,b,color = cols)
Out[117]: <[Link] at 0x166b79930>
In [107… a
Out[107]: array([22, 1, 25, 60, 22, 31, 78, 55, 41, 44])
In [108… b
Out[108]: array([71, 26, 15, 33, 76, 11, 44, 65, 35, 62])
In [109… C
Out[109]: array(['red', 'green', 'blue', 'cyan', 'megenta', 'yellow', 'hotpink',
'yellowgreen', 'brown', 'red'], dtype='<U11')
In [115… a = [Link]([10,20,34,56,67,78,89])
b = [Link]([20,30,45,12,35,46,67])
colors = [Link](["red","blue","m",'cyan',"brown","black","green"])
[Link](a,b,c=colors)
Out[115]: <[Link] at 0x1669eab60>
In [119… a = [Link]([10,20,34,56,67,78,89])
b = [Link]([20,30,45,12,35,46,67])
colors = [Link](["red","blue","m",'cyan',"brown","black","green"])
sizes = [Link]([10,20,30,40,50,60,70])
[Link](a,b,c=colors,s=sizes)
[Link]()
In [125… a = [Link]([10,20,34,56,67,78,89])
b = [Link]([20,30,45,12,35,46,67])
colors = [Link](["red","blue","m",'cyan',"brown","black","green"])
sizes = [Link]([10,20,30,40,50,60,70])
[Link](a,b,c=colors,s=sizes,alpha=0.5)
[Link]()
In [129… #plot a scatter plot for two array variable containig 100 random numbers in each with
#transperancy 0.7
a = [Link](100)
b = [Link](100)
[Link](a,b,color = 'red',alpha=0.4)
[Link]()
Pie Charts
In [131… a = [Link]([25,23,42,10])
[Link](a)
[Link]()
In [132… a = [Link]([25,23,42,10])
L = [Link](['Ecommerce','Dining','Clothing','Online'])
[Link](a,labels=L)
[Link]()
In [133… a = [Link]([25,23,42,10])
L = [Link](['Ecommerce','Dining','Clothing','Online'])
[Link](a,labels=L,startangle = 90)
[Link]()
In [137… a = [Link]([25,23,42,10])
L = [Link](['Ecommerce','Dining','Clothing','Online'])
[Link](a,labels=L,startangle = 90,explode = [0,0.09,0,0])
[Link]()
In [138… a = [Link]([25,23,42,10])
L = [Link](['Ecommerce','Dining','Clothing','Online'])
[Link](a,labels=L,startangle = 90,explode = [0,0.09,0,0],shadow=True)
[Link]()
In [140… a = [Link]([25,23,42,10])
L = [Link](['Ecommerce','Dining','Clothing','Online'])
C =[Link](['hotpink','yellowgreen','brown','green'])
[Link](a,labels=L,startangle = 90,colors = C, explode = [0,0.09,0,0],shadow=True)
[Link]()
In [142… a = [Link]([25,23,42,10])
L = [Link](['Ecommerce','Dining','Clothing','Online'])
C =[Link](['hotpink','yellowgreen','brown','green'])
[Link](a,labels=L,startangle = 90,colors = C, explode = [0,0.09,0,0],shadow=True)
[Link](loc='upper right')
[Link]()
In [143… a = [Link]([25,23,42,10])
L = [Link](['Ecommerce','Dining','Clothing','Online'])
C =[Link](['hotpink','yellowgreen','brown','green'])
[Link](a,labels=L,startangle = 90,colors = C, explode = [0,0.09,0,0],shadow=True)
[Link](loc='upper right',title='Spend Analysis')
[Link]()
In [147… a = [Link]([25,23,42,10])
L = [Link](['Ecommerce','Dining','Clothing','Online'])
C =[Link](['hotpink','yellowgreen','brown','green'])
[Link](a,labels=L,startangle = 90,colors = C, explode = [0,0.09,0,0],shadow=True,autopct='%1.2f%%')
[Link](loc='upper right',title='Spend Analysis')
[Link]()
In [148… df
Out[148]: survived pclass sex age sibsp parch fare embarked class who adult_male embark_town alive alone
0 0 3 male 22.000000 1 0 7.2500 S Third man True Southampton no False
1 1 1 female 38.000000 1 0 71.2833 C First woman False Cherbourg yes False
2 1 3 female 26.000000 0 0 7.9250 S Third woman False Southampton yes True
3 1 1 female 35.000000 1 0 53.1000 S First woman False Southampton yes False
4 0 3 male 35.000000 0 0 8.0500 S Third man True Southampton no True
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
886 0 2 male 27.000000 0 0 13.0000 S Second man True Southampton no True
887 1 1 female 19.000000 0 0 30.0000 S First woman False Southampton yes True
888 0 3 female 29.699118 1 2 23.4500 S Third woman False Southampton no False
889 1 1 male 26.000000 0 0 30.0000 C First man True Cherbourg yes True
890 0 3 male 32.000000 0 0 7.7500 Q Third man True Queenstown no True
891 rows × 14 columns
In [149… #plot pie chart for showing percentage of passengers who boarded in first, second , third
# class in titanic
In [155… df['class'].value_counts().plot(kind='pie',autopct='%1.2f%%')
Out[155]: <Axes: ylabel='count'>
In [153… df['class'].value_counts()
Out[153]: class
Third 491
First 216
Second 184
Name: count, dtype: int64
In [160… #plot a pie chart for showing percentages of people in age group 19 -23, 23 to 35, 35 - max
ages = df['age'].astype(int)
In [180… ages[ages>19].count()
Out[180]: 727
In [182… ages[ages>35].count()
Out[182]: 217
In [185… ages[ages<35].count()
Out[185]: 656
In [214… ag1 = df[(df['age']>19) & (df['age']<23)]
In [204… ag2 = df[(df['age']>23) & (df['age']<35)]
ag3 = df[(df['age']>35) ]
In [216… ag1 = ag1['age'].count()
ag2 = ag2['age'].count()
ag3 = ag3['age'].count()
In [221… [Link]([ag1,ag2,ag3],labels= ['age group-1','age Group-2','age Group-3'],autopct='%1.2f%%')
[Link]()
[Link]()
Histogram
In [222… a = [Link](1000)
In [226… [Link](a,bins=10, color='lightgreen',edgecolor='blue')
[Link]()
In [227… ages = [Link]([12,13,45,67,89,76,54,32,23,24,25,26,76,67,56,57,46,30,82,31,90,20,34,12,9,10])
In [239… [Link](ages,bins =20,color='skyblue',edgecolor='red')
[Link]()
In [237… df['age'].astype(int).plot(kind='hist',color = 'lightgreen',bins=20,edgecolor='red')
Out[237]: <Axes: ylabel='Frequency'>
In [240… #plot an of of 10000 random values in 10 bins with edgecolor blue and bar color yellowgreen
In [242… a = [Link](10000)
[Link](a, color = 'yellowgreen',edgecolor = 'blue')
[Link]()
In [246… # plot histogram or fare in titanic dataset with 10 bins
In [249… df['fare'].plot(kind='hist',bins=10,color='brown',edgecolor='yellowgreen')
[Link]()
In [251… #plot histogram for age in titanic dataset
df['age'].plot(kind='hist',bins=20,color='lightgreen',edgecolor='red')
[Link]()
area plots
In [252… a = [Link]([10,20,30,40,50])
b = [Link]([5,10,15,20,25])
In [253… [Link](a,b)
plt.fill_between(a,b)
[Link]()
In [257… x = [Link](1,10,1000)
y = [Link](x)
[Link](x,y)
plt.fill_between(x,y,color='red')
[Link]()
In [258… df1 = [Link]({'ID':[101,102,103,104,105],'Marks':[98,99,89,76,57]})
In [259… df1
Out[259]: ID Marks
0 101 98
1 102 99
2 103 89
3 104 76
4 105 57
In [260… df2 = [Link]({'ID':[101,102,105,106,107],'Marks':[89,67,34,56,90]})
In [261… df2
Out[261]: ID Marks
0 101 89
1 102 67
2 105 34
3 106 56
4 107 90
In [262… #inner join
In [264… out = [Link](df1,df2,on = 'ID',how='inner')
In [265… out
Out[265]: ID Marks_x Marks_y
0 101 98 89
1 102 99 67
2 105 57 34
In [266… out2 = [Link](df1,df2,on = 'ID',how='outer')
In [267… out2
Out[267]: ID Marks_x Marks_y
0 101 98.0 89.0
1 102 99.0 67.0
2 103 89.0 NaN
3 104 76.0 NaN
4 105 57.0 34.0
5 106 NaN 56.0
6 107 NaN 90.0
In [268… out3 = [Link](df1,df2,on='ID',how='left')
In [269… out3
Out[269]: ID Marks_x Marks_y
0 101 98 89.0
1 102 99 67.0
2 103 89 NaN
3 104 76 NaN
4 105 57 34.0
In [270… out4 = [Link](df1,df2,on='ID',how='right')
out4
Out[270]: ID Marks_x Marks_y
0 101 98.0 89
1 102 99.0 67
2 105 57.0 34
3 106 NaN 56
4 107 NaN 90
In [ ]: