Matplotlib Python
Matplotlib is a plotting library for the Python
programming language & its numerical
mathematical extension Numpy. It provides an object-oriented API for embedding
plots into applications using general-purpose GUI-Tool kits.
I will cover all the basic concepts of the Matplotlib
with an overview of each graph’s. Before directly diving into this please check
out my blog regarding to Numpy & Pandas where I had covered all the basics
which will help you to understand the further concept easily.
To use matplotlib you must install the matplotlib
library using pip command.
To import matplotlib we have to just type :
import matplotlib.pyplot
as plt
Now open you Jupyter Notebook & Lets start to code
:
Code 1 : Basic
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x=[1,2,3]
y=[5,6,7]
plt.plot(x,y)
plt.show
output :
Legends : Legends is basically the index where we describe which design serve what purpose.
Title : Which describes our graph
output :
Code 2 : Label , Legends
& Title
Label : Name which we give to the x & y axis.
For label = label="_val_"
For X-axis = xlabel()
For Y-axis = ylabel()
Legends : Legends is basically the index where we describe which design serve what purpose.
Title : Which describes our graph
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
x=[11,12,13]
y=[9,16,7]
x1=[10,12,13]
x2=[15,6,17]
plt.plot(x,y,label="First_Line")
plt.plot(x1,y1,label="Second_Line")
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
plt.title("Graph 2 for practise \nCode2")
plt.legend()
plt.show()
output :
Code 3 : Bar Chat v/s
Histogram
(i)Bar Chat : Compare
Things
(ii)Histogram : Show Distribution of Values of
single Parameter
(i)Bar Charts Example :
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
x=[2,4,6,8,10]
y=[6,7,8,9,10]
x1=[3,5,4,3,4]
y1=[8,6,7,8,5]
plt.bar(x,y,label='Bar_1',color='red')
plt.bar(x,y,label='Bar_2',color='blue')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Bar Chart Example')
plt.legend()
plt.show()
output :
(ii)Histogram Charts Example :
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
marks=[12,43,4,6,56,23,37,9,70,65,55,82,91,100]
range=[0,10,20,30,40,50,60,70,80,90,100]
plt.hist(marks,range,histtype='bar',rwidth=0.8,label='Scores of Students')
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
plt.title('Histogram Chart Example (Bar type)')
plt.legend()
plt.show()
output :
Code 4 : Scatter Plot Graph
A Scatter Plot is a
type of plot or mathematical diagram using Cartesian coordinates to display values
for typically 2 variables for a set of data.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
x=[1,2,8,9,5]
y=[6,7,3,4,10]
plt.scatter(x,y,label='val',color='black',marker='*',s=200)
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
plt.title('Scatter Plot Graph')
plt.legend()
plt.show()
output :
Code 5 : Stack Plot
A stack plot is a plot that shows
the whole data set with easy visualization of how each part makes up the whole.
Each constituent of the stack plot is stacked on top of each other.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
Total = [80,90,100,60,50]
english = [30,40,50,20,10]
maths = [10,20,20,30,30]
history = [40,30,30,10,10]
plt.plot([],[],color='blue',label='english',linewidth=5)
plt.plot([],[],color='green',label='maths',linewidth=5)
plt.plot([],[],color='yellow',label='history',linewidth=5)
plt.stackplot(Total,english,maths,history,colors=['blue', 'green', 'yellow'])
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
plt.title('Stack Graph')
plt.legend()
plt.show()
output :
Code 6 :Pie Charts Example
A Pie Chart is the circular statistical graphic, which is divided
into slices to illustrate numerical proportion.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
Total = [80,90,100,60,50]
english = [30,40,50,20,10]
maths = [10,20,20,30,30]
history = [40,30,30,10,10]
slices=[7,2,13]
x=['english','maths','history']
cols=['blue', 'green', 'yellow']
plt.pie(slices, labels=x,startangle=90,shadow=True,explode=(0,0,0.05),autopct=''%1.1f%%'')
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
plt.title('Pie')
plt.show()
output :
In descriptive statistics, a boxplot is a method for
graphically depicting groups of numerical data through their quartiles. Box
plots may also have lines extending from the boxes indicating variability
outside the upper & lower quartiles, hence the terms box-and-whisker plot
diagram.
Theoretical Knowledge :
From Q1 to Q3 the number of values comes between them is consider its height.
eg :
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
data=pd.read_csv(r"D:\MY_ML\home.csv")
print(data)
tarea=data.Area
tprice=data.Price
x=list([tarea,tprice])
plt.boxplot(x,showmeans=True)
plt.grid(True)
plt.show()
output :
No comments:
No Spamming and No Offensive Language