0% found this document useful (0 votes)
12 views1 page

Matplotlib_tutorial

The document provides a comprehensive guide on using Matplotlib for data visualization in Python, covering various types of plots including line plots, bar plots, and scatter plots. It includes code examples for creating these plots, customizing their appearance, and handling data from a CSV file. Additionally, it demonstrates how to use subplots and different color and marker options in visualizations.

Uploaded by

Satya Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Matplotlib_tutorial

The document provides a comprehensive guide on using Matplotlib for data visualization in Python, covering various types of plots including line plots, bar plots, and scatter plots. It includes code examples for creating these plots, customizing their appearance, and handling data from a CSV file. Additionally, it demonstrates how to use subplots and different color and marker options in visualizations.

Uploaded by

Satya Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

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/masked.py:60: UserWarning: Pandas requires version '1.3.6' or newer of 'bottleneck' (ve


rsion '1.3.5' currently installed).
from pandas.core import (

Line Plot
In [2]: a = np.array([1,2,3,4,5,6,7,8])

In [3]: plt.plot(a)

Out[3]: [<matplotlib.lines.Line2D at 0x14acb3dc0>]

In [4]: a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
plt.plot(a,b)

Out[4]: [<matplotlib.lines.Line2D at 0x14adeadd0>]

In [5]: a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
plt.plot(a,b,'r')

Out[5]: [<matplotlib.lines.Line2D at 0x14af7d240>]

'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')

Out[6]: [<matplotlib.lines.Line2D at 0x14afeb7c0>]

In [7]: a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
plt.plot(a,b,'green')

Out[7]: [<matplotlib.lines.Line2D at 0x14b072110>]

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 [10]: plt.figure(figsize=(8,3)) #size = width,hight


x = np.linspace(1,100,1000)
y = np.sin(x)
plt.plot(x,y,'m')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.show()

In [11]: plt.figure(figsize=(8,3)) #size = width,hight


x = np.linspace(1,100,1000)
y = np.sin(x)
plt.plot(x,y,'g')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.grid()
plt.show()

In [12]: plt.figure(figsize=(8,3)) #size = width,hight


x = np.linspace(1,100,1000)
y = np.sin(x)
plt.plot(x,y,'g')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.title('Sinewave')
plt.grid()
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()

In [15]: plt.figure(figsize=(8,3)) #size = width,hight


x = np.linspace(1,100,1000)
y = np.sin(x)
plt.plot(x,y,'b:')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.title('Sinewave')
plt.grid()
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()

In [21]: #plot multiple line plots in single figure window

In [22]: plt.figure(figsize=(8,4)) #size = width,hight


x = np.linspace(0,10,1000)
y = np.sin(x)
z = np.cos(x)
plt.plot(x,y,'g-',label='sinewave')
plt.plot(x,z,'r-',label='cosine wave')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.title('Sinewave')
plt.grid()
plt.legend(loc='upper right')
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()

In [25]: plt.figure(figsize=(6,3)) #size = width,hight


x = np.linspace(0,10,1000)
y = np.sin(x)
plt.plot(x,y,color='#76d7c4',linewidth=4)
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.title('Sinewave')
plt.grid()
plt.show()

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')

In [31]: df.drop('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]: plt.figure(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]: plt.figure(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]: plt.figure(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/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)

Out[117]: <matplotlib.collections.PathCollection 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 = 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)

Out[115]: <matplotlib.collections.PathCollection at 0x1669eab60>

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

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… plt.pie([ag1,ag2,ag3],labels= ['age group-1','age Group-2','age Group-3'],autopct='%1.2f%%')


plt.legend()
plt.show()

Histogram
In [222… a = np.random.randn(1000)

In [226… plt.hist(a,bins=10, color='lightgreen',edgecolor='blue')


plt.show()

In [227… ages = np.array([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… plt.hist(ages,bins =20,color='skyblue',edgecolor='red')


plt.show()

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 = np.random.random(10000)
plt.hist(a, color = 'yellowgreen',edgecolor = 'blue')
plt.show()

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')
plt.show()

In [251… #plot histogram for age in titanic dataset


df['age'].plot(kind='hist',bins=20,color='lightgreen',edgecolor='red')
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 [258… df1 = pd.DataFrame({'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 = pd.DataFrame({'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 = pd.merge(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 = pd.merge(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 = pd.merge(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 = pd.merge(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 [ ]:

You might also like