matpl o
tlib
WHAT IS
MATPLOTLIB
Matplotlib is a plotting library for the Python programming language and its numerical mathematics
extension NumPy.
WHY MATPLOTLIB
Matplotlib makes easy things easy and hard things possible. Create publication quality plots. Make
interactive figures that can zoom, pan, update
Istallation&importing matplotlib
we can install
we can use it simply
matplotlib
by using a command
inside anaconda
called
"pip install
environment
matplotlib"
0
We can import matplotlib by
saying "import
matplotlib.pyplot as plt"
Types of plots 0
1
Bar
graph
in matplotlib 02 sc atter
plot
03 Histogram
04 pie
c hart
plt. plot()
The basic syntax for creating line
plots is plt. plot(x,y), where x and y are
arrays of the same length that specify
the (x, y) pairs that form the line
marker & size
we can use different types
of markers to the points
ex:marker ='o'
marker size hepls to show
different sizes of points based
on the data ex:ms=10
marker edge marker face color
colour colour attribute
we can also change the we can also change the if we want to change the
marker edge colour using marker colour using entire colour we use
mec(marker edge color) mfc(marker face colour) 'color'attribute
line style
by default the line style is
solid , to change the line style
we use an argument called
as 'ls' (line style)
ex:linewidth='5.6
LABLES&
TITLESlables labl
e
lables play a imp role in graph
we add l a b l e s for better understanding of
graph
p lt. xlab le (" X - A xis ")
p lt. ylab le (" Y - A xis ")
we get a graph with labled axis
TITLE
Titles
Subplot
subplot function
this t i tle func tio n ad d s t i tle to the
helps to create multiple plots as
graphs at once g rap h
p lt. t i tle (' g rap h with matp lo tlib ’ )
plt.subplot(2,2,1)
he re we g e t a g rap h with t i tle
Bar 2
5
graphs
With pyplot we c an use the func tion to draw
the bar graph which are used to represent 20
c ategoral variables
ex:
x=np.array(["A","B","C "])
y=np.array([6,3,9]) 1
plt.bar(x,y) 5
the output will be a bar graph
if we need a horizontal graph
ex: plt.barh(x,y) 10
we can also change the color, width&also the
design of the graph
5
ex:
x=np.array(["A","B","
C"])
y=np.array([6,3,9])
0
plt.bar(x,y,color='green',width=0.4) Item Item Item Item 4 Item
bars[0].sethatch('/') 1 2 3 5
bars[1].sethatch('o')
bars[2].sethatch('*')
SCATER
PLOTS
To draw scatter plots we use scatterplot function this scatter plot plots one dot for each
observation.
x=np.array([4,8,11,9,10,6])
y= np.array([99,111,87,94,88,33])
plt.scatter(x,y) plt.show()
We can add two scatterplot functions &we can plot different colour points on graph.
The output will be a graph with two different coloured points on graph
By default the colours will be blue & orange we can also change the colour for each scatter
plot by passing it as a argument
plt.scatter(x,y,color="red")
We can also set different colours for each dot by using an array of colors we can also set
different sizes for each dot by using array of size
Histrogram
s It is a graph that show s frequenc y distribution
It is a graph show ing number of observations w ithin eac h
given interval c reating a random values
ex: x=np.random.normal(170,10,250)
( c reating a random values)
print(x)
output: this will generate 250 values around 170 with a
standard deviation of 10
To draw the histogram for the data
ex: x=np.random.normal(170,10,250)
plt.hist(
x)
plt.show
()
output: It creates an histogram,hist function is used to
create a histogram, the array is sent inside the hist function
PI EC HAR
T Pie charts helps in easy understanding of data
plt.pie(
y=np.array([55,20,6] )
y)
plt.show C ost C ost
() 3 1
33.3% 33.3
1.This will create a pie chart the pie chart draws each piece which can be %
3.We canasalso
called add the lables
an"wedge" byh "lable"
for eac value inparameter
an array
the lable
2.By parameter
default tehe plotmust befrom
starts an array with
the "x" onemoves
axist lable "c ounter-c loc kwise"
witheach wedge
y=np.array([55,20,6] )
l=["A","B","C"]
plt.pie(y,lables=l)
plt.show()
C ost
2
33.3%
To change the start angle we use can use start angle
parameter
y=np.array([55,20,6] )
anu