Matplotlib_tutorial
Matplotlib_tutorial
Line Plot
In [2]: a = np.array([1,2,3,4,5,6,7,8])
In [3]: plt.plot(a)
In [4]: a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
plt.plot(a,b)
In [5]: a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
plt.plot(a,b,'r')
'r'. - red 'g'. - green 'b'. - blue 'c'. - cyan 'm'. - megenta 'y'. - yellow 'w'. - white 'k'. - black
In [6]: a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
plt.plot(a,b,'red')
In [7]: a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
plt.plot(a,b,'green')
In [8]: x = np.linspace(1,100,1000)
y = np.sin(x)
plt.plot(x,y,'m')
plt.show()
In [9]: x = np.linspace(1,100,1000)
y = np.sin(x)
plt.plot(x,y,'m')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.show()
In [13]: plt.figure(figsize=(4,3))
a = np.array([10,45,23,67,12])
plt.plot(a,'b')
plt.show()
In [14]: plt.figure(figsize=(4,3))
a = np.array([10,45,23,67,12])
plt.plot(a,'r:*')
plt.show()
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]: plt.figure(figsize=(4,3))
a = np.array([10,45,23,67,12])
plt.plot(a,'r:*')
plt.show()
In [17]: plt.figure(figsize=(4,3))
a = np.array([10,45,23,67,12])
plt.plot(a,'b-.P')
plt.show()
In [18]: plt.figure(figsize=(4,3))
a = np.array([10,45,23,67,12])
plt.plot(a,marker='D',mfc='r',mec='b')
plt.show()
In [19]: plt.figure(figsize=(4,3))
a = np.array([10,45,23,67,12])
plt.plot(a,marker='D',ms=10,mfc='r',mec='b')
plt.show()
In [20]: plt.figure(figsize=(4,3))
a = np.array([10,45,23,67,12])
plt.plot(a,color='y',marker='D',ms=10,mfc='r',mec='k')
plt.show()
subplots
In [23]: plt.figure(figsize=(6,3))
x = np.linspace(0,10,1000)
y = np.sin(x)
plt.subplot(1,2,1)
plt.plot(x,y,'r')
plt.grid()
z = np.cos(x)
plt.subplot(1,2,2)
plt.plot(x,z,'g')
plt.grid()
In [24]: plt.figure(figsize=(6,3))
x = np.linspace(0,10,1000)
y = np.sin(x)
plt.subplot(2,2,1)
plt.plot(x,y,'r')
plt.grid()
z = np.cos(x)
plt.subplot(2,2,2)
plt.plot(x,z,'g')
plt.grid()
plt.subplot(2,2,3)
plt.plot(x-20,y,'y')
plt.grid()
plt.subplot(2,2,4)
plt.plot(x-40,y,'m')
plt.grid()
Bar plot
In [ ]:
In [26]: a =['satya','sekhar','jayanth','Vishnu']
b =[25,50,75,90]
plt.figure(figsize=(6,3)) #size = width,hight
plt.bar(a,b)
plt.show()
In [27]: a =['satya','sekhar','jayanth','Vishnu']
b =[25,50,75,90]
plt.figure(figsize=(6,3)) #size = width,hight
plt.bar(a,b,color='m')
plt.show()
In [28]: a =['satya','sekhar','jayanth','Vishnu']
b =[25,50,75,90]
plt.figure(figsize=(6,3)) #size = width,hight
plt.bar(a,b,color='m',width=0.4)
plt.show()
In [29]: a =['satya','sekhar','jayanth','Vishnu']
b =[25,50,75,90]
plt.figure(figsize=(6,3)) #size = width,hight
plt.barh(a,b)
plt.show()
In [30]: df = pd.read_csv('cleaned_titanic.csv')
Out[31]: survived pclass sex age sibsp parch fare embarked class who adult_male embark_town alive alone
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
887 1 1 female 19.000000 0 0 30.0000 S First woman False Southampton yes True
889 1 1 male 26.000000 0 0 30.0000 C First man True Cherbourg yes True
In [41]: plt.figure(figsize=(4,2))
df['sex'].value_counts().plot(kind='bar',color = 'r',width=0.2)
In [42]: #plot a bar plot to show how many mem, women and child passengers were onboarded into ship?
In [44]: plt.figure(figsize=(4,2))
df['who'].value_counts().plot(kind='bar',color = 'm',width=0.2)
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')
In [57]: df[df['sex']=='female'].groupby('survived')['sex'].count().plot(kind='bar',color='c')
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')
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]: plt.figure(figsize=(4,2))
df[df['adult_male']==True].groupby('embark_town')['adult_male'].count()[1:].plot(kind='bar')
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/711970834.py:1: 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]: #plt.scatter()
In [85]: plt.figure(figsize=(4,4))
a = np.array([34,56,78,90,12,34,52])
b = np.array([45,67,89,23,90,45,12])
plt.scatter(a,b)
plt.show()
In [86]: a = np.random.randint(1,100,30)
In [89]: b = np.random.randint(10,100,30)
plt.scatter(a,b,color = 'hotpink')
plt.show()
In [90]: a = np.random.randint(1,100,30)
b = np.random.randint(10,100,30)
c = np.random.randint(1,100,20)
d = np.random.randint(10,100,20)
plt.scatter(a,b,color = 'hotpink')
plt.scatter(c,d,color='yellowgreen')
plt.show()
In [117… a = np.random.randint(1,100,10)
b = np.random.randint(10,100,10)
cols = ['red','green','blue','cyan','magenta','yellow','hotpink','yellowgreen','brown','red']
plt.scatter(a,b,color = cols)
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
In [115… a = np.array([10,20,34,56,67,78,89])
b = np.array([20,30,45,12,35,46,67])
colors = np.array(["red","blue","m",'cyan',"brown","black","green"])
plt.scatter(a,b,c=colors)
In [119… a = np.array([10,20,34,56,67,78,89])
b = np.array([20,30,45,12,35,46,67])
colors = np.array(["red","blue","m",'cyan',"brown","black","green"])
sizes = np.array([10,20,30,40,50,60,70])
plt.scatter(a,b,c=colors,s=sizes)
plt.show()
In [125… a = np.array([10,20,34,56,67,78,89])
b = np.array([20,30,45,12,35,46,67])
colors = np.array(["red","blue","m",'cyan',"brown","black","green"])
sizes = np.array([10,20,30,40,50,60,70])
plt.scatter(a,b,c=colors,s=sizes,alpha=0.5)
plt.show()
In [129… #plot a scatter plot for two array variable containig 100 random numbers in each with
#transperancy 0.7
a = np.random.random(100)
b = np.random.random(100)
plt.scatter(a,b,color = 'red',alpha=0.4)
plt.show()
Pie Charts
In [131… a = np.array([25,23,42,10])
plt.pie(a)
plt.show()
In [132… a = np.array([25,23,42,10])
L = np.array(['Ecommerce','Dining','Clothing','Online'])
plt.pie(a,labels=L)
plt.show()
In [133… a = np.array([25,23,42,10])
L = np.array(['Ecommerce','Dining','Clothing','Online'])
plt.pie(a,labels=L,startangle = 90)
plt.show()
In [137… a = np.array([25,23,42,10])
L = np.array(['Ecommerce','Dining','Clothing','Online'])
plt.pie(a,labels=L,startangle = 90,explode = [0,0.09,0,0])
plt.show()
In [138… a = np.array([25,23,42,10])
L = np.array(['Ecommerce','Dining','Clothing','Online'])
plt.pie(a,labels=L,startangle = 90,explode = [0,0.09,0,0],shadow=True)
plt.show()
In [140… a = np.array([25,23,42,10])
L = np.array(['Ecommerce','Dining','Clothing','Online'])
C =np.array(['hotpink','yellowgreen','brown','green'])
plt.pie(a,labels=L,startangle = 90,colors = C, explode = [0,0.09,0,0],shadow=True)
plt.show()
In [142… a = np.array([25,23,42,10])
L = np.array(['Ecommerce','Dining','Clothing','Online'])
C =np.array(['hotpink','yellowgreen','brown','green'])
plt.pie(a,labels=L,startangle = 90,colors = C, explode = [0,0.09,0,0],shadow=True)
plt.legend(loc='upper right')
plt.show()
In [143… a = np.array([25,23,42,10])
L = np.array(['Ecommerce','Dining','Clothing','Online'])
C =np.array(['hotpink','yellowgreen','brown','green'])
plt.pie(a,labels=L,startangle = 90,colors = C, explode = [0,0.09,0,0],shadow=True)
plt.legend(loc='upper right',title='Spend Analysis')
plt.show()
In [147… a = np.array([25,23,42,10])
L = np.array(['Ecommerce','Dining','Clothing','Online'])
C =np.array(['hotpink','yellowgreen','brown','green'])
plt.pie(a,labels=L,startangle = 90,colors = C, explode = [0,0.09,0,0],shadow=True,autopct='%1.2f%%')
plt.legend(loc='upper right',title='Spend Analysis')
plt.show()
In [148… df
Out[148]: survived pclass sex age sibsp parch fare embarked class who adult_male embark_town alive alone
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
887 1 1 female 19.000000 0 0 30.0000 S First woman False Southampton yes True
889 1 1 male 26.000000 0 0 30.0000 C First man True Cherbourg yes True
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%%')
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
Histogram
In [222… a = np.random.randn(1000)
In [240… #plot an of of 10000 random values in 10 bins with edgecolor blue and bar color yellowgreen
In [242… a = np.random.random(10000)
plt.hist(a, color = 'yellowgreen',edgecolor = 'blue')
plt.show()
In [249… df['fare'].plot(kind='hist',bins=10,color='brown',edgecolor='yellowgreen')
plt.show()
area plots
In [252… a = np.array([10,20,30,40,50])
b = np.array([5,10,15,20,25])
In [253… plt.plot(a,b)
plt.fill_between(a,b)
plt.show()
In [257… x = np.linspace(1,10,1000)
y = np.sin(x)
plt.plot(x,y)
plt.fill_between(x,y,color='red')
plt.show()
In [259… df1
Out[259]: ID Marks
0 101 98
1 102 99
2 103 89
3 104 76
4 105 57
In [261… df2
Out[261]: ID Marks
0 101 89
1 102 67
2 105 34
3 106 56
4 107 90
In [265… out
0 101 98 89
1 102 99 67
2 105 57 34
In [267… out2
In [269… out3
0 101 98 89.0
1 102 99 67.0
2 103 89 NaN
3 104 76 NaN
4 105 57 34.0
0 101 98.0 89
1 102 99.0 67
2 105 57.0 34
3 106 NaN 56
4 107 NaN 90
In [ ]: