SlideShare a Scribd company logo
matplotlib.pptxdsfdsfdsfdsdsfdsdfdsfsdf cvvf
Matplotlib
Matplotlib is a low level graph plotting library in
python that serves as a visualization utility.
Matplotlib
Created By- John D. Hunter
Developed- 2002
Release- 2003
How to install Matplotlib in windows
py -m pip install matplotlib
 Open Command Prompt in windows
 Than set user account
 And run follow command ( When computer is connected to
internet )
How to use Matplotlib
import matplotlib as mt
Check Version
import matplotlib as mt
Print(mt. version )
Output:
4.0
Matplotlib Pyplot
import matplotlib.pyplot as plt
Most of the Matplotlib utilities lies under the pyplot submodule, and are
usually imported under the plt alias:
Plotting x and y points
 The plot() function is used to draw points (markers) in a diagram.
 By default, the plot() function draws a line from point to point.
 The function takes parameters for specifying points in the diagram.
 Parameter 1 is an array containing the points on the x-axis (horizontal axis).
 Parameter 2 is an array containing the points on the y-axis (vertical axis)..
Plotting Line
import matplotlib.pyplot as plt
import numpy as np
x= np.array([0, 6])
y = np.array([0, 250])
Draw a line in a diagram from position (0,0) to position (6,250):
plt.plot(x, y)
plt.show()
Plotting without Line
import matplotlib.pyplot as plt
import numpy as np
x= np.array([0, 6])
y = np.array([0, 250])
To plot only the markers, you can use shortcut string notation parameter 'o', which
means 'rings'.
plt.plot(x, y, ‘o’)
plt.show()
Multiple Points
import matplotlib.pyplot as plt
import numpy as np
x= np.array([1, 2,6,8])
y = np.array([3,8,1,10])
plt.plot(x, y)
plt.show()
Default X-Axis
import matplotlib.pyplot as plt
import numpy as np
y = np.array([3,8,1,10,5,7])
If we do not specify the points in the x-axis, they will get the default values 0, 1, 2, 3,
(etc. depending on the length of the y-points.
plt.plot( y)
plt.show()
Matplotlib Markers
import matplotlib.pyplot as plt
import numpy as np
y = np.array([3,8,1,10])
You can use the keyword argument marker to emphasize each point with a specified
marker:
plt.plot( y, marker=‘o’)
plt.show()
Markers Reference
Marker Description
'o' Circle
'*' Star
'.' Point
'x' X
'X' X (filled)
'+' Plus
'P' Plus (filled)
's' Square
'D' Diamond
Marker Description
'd' Diamond (thin)
'p' Pentagon
'H' Hexagon
'h' Hexagon
'v' Triangle Down
'^' Triangle Up
'<' Triangle Left
'>' Triangle Right
Format Strings fmt
import matplotlib.pyplot as plt
import numpy as np
y = np.array([3,8,1,10])
plt.plot( y,
‘o:r’) plt.show()
This parameter is also called fmt, and is written with this syntax:
marker|line|color
Line Reference
Line Syntax Description
‘-' Solid Line
‘:' Dotted Line
‘--' Dashed Line
‘-.' Dashed/ Dotted Line
Color Reference
Color Syntax Description
‘r' Red
‘g' Green
‘b' Blue
‘c' Cyan
‘m’ Magenta
‘y’ Yellow
‘k’ Black
‘w’ White
Markers Size
import matplotlib.pyplot as plt
import numpy as np
y = np.array([3,8,1,10])
plt.plot( y, marker= ‘o’,
ms= 20)
You can use the keyword argument markersize or the shorter version, ms to set the
size of the markers:
plt.show()
Markers Edge Size
import matplotlib.pyplot as plt
import numpy as np
y = np.array([3,8,1,10])
You can use the keyword argument markeredgecolor or the shorter mec to set the
color of the edge of the markers:
plt.plot( y,
mec=‘r’) plt.show()
marker= ‘o’, ms= 20,
Markers Face Size
import matplotlib.pyplot as plt
import numpy as np
y = np.array([3,8,1,10])
You can use the keyword argument markerfacecolor or the shorter mfc to set the
color of the edge of the markers:
plt.plot( y,
mfc=‘r’)
plt.show()
marker= ‘o’, ms= 20,
Linestyle Argument
import matplotlib.pyplot as plt
import numpy as np
y = np.array([3,8,1,10])
plt.plot( y, linestyle=‘dotted’)
You can use the keyword argument linestyle, or shorter ls, to change the style of the
plotted line
plt.show()
Line Reference
ls Syntax linestyle Syntax
‘-' ‘solid’
‘:' ‘dotted’
‘--' ‘dashed’
‘-.' ‘dashdot’
Line Color Argument
import matplotlib.pyplot as plt
import numpy as np
y = np.array([3,8,1,10])
plt.plot( y, color=‘r’)
You can use the keyword argument color or the shorter c to set the color of the line:
plt.show()
Line width Argument
import matplotlib.pyplot as plt
import numpy as np
y = np.array([3,8,1,10])
plt.plot( y, linewidth=’20.6’)
You can use the keyword argument linewidth or the shorter lw to change the width of
the line
plt.show()
Multiple width Argument
import matplotlib.pyplot as plt
import numpy as np
x1 = np.array([0,1,2,3])
y1 = np.array([3,8,1,10])
x2 = np.array([0,1,2,3])
y2 = np.array([6,2,7,11])
plt.plot( x1,y1,x2,y2)
You can use the keyword argument linewidth or the shorter lw to change the width of
the line
plt.show()
Create Labels
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0,1,2,3,4,5])
y = np.array([0,8,12,20,26,38])
plt.plot(x,y)
plt.xlabel(“Overs’)
With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x-
and y-axis.
plt.ylabel(“Runs’)
plt.show()
Create Title
matplotlib.pyplot as plt
import numpy as np
x =
np.array([0,1,2,3,4,5])
y =
np.array([0,8,12,20,26,3
8])
plt.plot(x,y)
plt.xlabel(“Overs’)
plt.ylabel(“Runs’)
With Pyplot, you can use the title() function to set a title for the plot.
Sport Data
Plt.title(“Sport Data”)
plt.show()
Set Font Properties for Title and Labels
You can use the fontdict parameter in xlabel(), ylabel(), and title() to set font
properties for the title and labels.
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0,1,2,3,4,5])
y = np.array([0,8,12,20,26,38])
font1 = {'family':'serif','color':'blue','size':20}
font2 = {'family':'serif','color':'darkred','size':15}
plt.plot(x,y)
plt.xlabel(“Overs’,fontdict=font2)
plt.ylabel(“Runs’ ,fontdict=font2)
Plt.title(“Sport Data” ,fontdict=font1)
plt.show()
Set Font Properties for Title and Labels
Position the Title
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0,1,2,3,4,5])
y = np.array([0,8,12,20,26,38])
plt.plot(x,y)
plt.xlabel(“Overs’)
plt.ylabel(“Runs’)
You can use the loc parameter in title() to position the title.
Legal values are: 'left', 'right', and 'center'. Default value is 'center'.
Plt.title(“Sport Data”, loc=‘left’)
plt.show()
Add Grid Lines to a Plot
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0,1,2,3,4,5])
y = np.array([0,8,12,20,26,38])
plt.plot(x,y)
plt.xlabel(“Overs’)
plt.ylabel(“Runs’)
Plt.title(“Sport
Data”, loc=‘left’)
With Pyplot, you can use the grid() function to add grid lines to the plot.
Plt.grid()
plt.show()
Display Multiple Plots
With the subplot() function you can draw multiple plots in one figure:
 The subplot() function takes three arguments that describes the layout of the
figure.
 The layout is organized in rows and columns, which are represented by the first and
second argument.
 The third argument represents the index of the current plot.
Display Multiple Plots
import matplotlib.pyplot as plt
import numpy as np
#plot 1
x = np.array([0,1,2,3])
y = np.array([3,8,1,10])
plt.subplot(1,2,1)
plt.plot(x,y)
#plot 2
x = np.array([0,1,2,3])
y =
np.array([10,20,30,40])
plt.subplot(1,2,2)
plt.plot(x,y)
plt.show()
Display Multiple Plots
import matplotlib.pyplot as plt
import numpy as np
#plot 1
x = np.array([0,1,2,3])
y = np.array([3,8,1,10])
plt.subplot(2,1,1)
plt.plot(x,y)
#plot 2
x = np.array([0,1,2,3])
y =
np.array([10,20,30,40])
plt.subplot(2,1,2)
plt.plot(x,y)
plt.show()
Super Title
import matplotlib.pyplot as plt
import numpy as np
#plot 1
x = np.array([0,1,2,3])
y = np.array([3,8,1,10])
plt.subplot(2,1,1)
plt.plot(x,y)
#plot 2
x = np.array([0,1,2,3])
y =
np.array([10,20,30,40])
plt.subplot(2,2,1)
plt.plot(x,y)
You can add a title to the entire figure with the suptitle() function:
plt.suptitle(“My Data”)
plt.show()
Creating Scatter Plots
 With Pyplot, you can use the scatter() function to draw a scatter plot.
 The scatter() function plots one dot for each observation. It needs
two arrays of the same length, one for the values of the x-axis, and
one for values on the y-axis:
Scatter Plots
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0,1,2,3,4,5])
y = np.array([0,8,12,20,26,38])
plt.scatter(x,y)
plt.show()
Compare Plots
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0,1,2,3,4,5])
y = np.array([0,2,8,1,14,7])
plt.scatter(x,y,color='red')
x =
np.array([12,6,8,11,8,3])
y =
np.array([5,6,3,7,17,19])
plt.scatter(x,y,color='green
')
plt.show()
Color each dots
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0,1,2,3,4,5])
y = np.array([0,2,8,1,14,7])
mycolor=['red','green','purple','lime','aqua','
yellow']
plt.scatter(x,y,color= mycolor )
plt.show()
Size
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0,1,2,3,4,5])
y = np.array([0,2,8,1,14,7])
mycolor=['red','green','purple','lime','aqua','
yellow']
size=[10,60,120,80,20,190]
plt.scatter(x,y,color= mycolor, s=size )
You can change the size of the dots with the s argument.
plt.show()
Alpha
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0,1,2,3,4,5])
y = np.array([0,2,8,1,14,7])
mycolor=['red','green','purple','lime','aqua','
yellow']
size=[10,60,120,80,20,190]
plt.scatter(x,y,color= mycolor, s=size,
alpha=0.3 )
You can adjust the transparency of the dots with the alpha argument.
plt.show()
Create Bar
import matplotlib.pyplot as plt
import numpy as np
x = np.array([‘A’,’B’,’C’,’D’])
y = np.array([3,8,1,10])
plt.bar(x,y)
With Pyplot, you can use the bar() function to draw bar graphs:
plt.show()
Create Horizontal Bar
import matplotlib.pyplot as plt
import numpy as np
x = np.array([‘A’,’B’,’C’,’D’])
y = np.array([3,8,1,10])
plt.barh(x,y)
If you want the bars to be displayed horizontally instead of vertically, use the barh()
function:
plt.show()
Bar Width
import matplotlib.pyplot as plt
import numpy as np
x = np.array([‘A’,’B’,’C’,’D’])
y = np.array([3,8,1,10])
The bar() takes the keyword argument width to set the width of the bars:
plt.barh(x,y,width=0.1)
plt.show()
Histogram
A histogram is a graph showing frequency distributions.
It is a graph showing the number of observations within each given interval.
In Matplotlib, we use the hist() function to create histograms.
The hist() function will read the array and produce a histogram:
Histogram
import matplotlib.pyplot as plt
import numpy as np
x = np.random.normal(170,10,250)
plt.hist(x)
plt.show()
Creating Pie Charts
import matplotlib.pyplot as plt
import numpy as np
x = np.array([35,25,25,15])
plt.pie(y)
plt.show()
With Pyplot, you can use the pie() function to draw pie charts:
Labels
import matplotlib.pyplot as plt
import numpy as np
x = np.array([35,25,25,15])
mylabels = ["Apples", "Bananas",
"Cherries", "Dates"]
plt.pie(y, labels=mylabels)
Add labels to the pie chart with the label parameter.
The label parameter must be an array with one label for each wedge:
plt.show()
Start Angle
import matplotlib.pyplot as plt
import numpy as np
x = np.array([35,25,25,15])
mylabels = ["Apples", "Bananas",
"Cherries", "Dates"]
plt.pie(y,
labels=mylabels,startangle=90)
As mentioned the default start angle is at the x-axis, but you can change the start
angle by specifying a startangle parameter.
plt.show()
Explode
import matplotlib.pyplot as plt
import numpy as np
x = np.array([35,25,25,15])
mylabels = ["Apples", "Bananas", "Cherries",
"Dates"]
myexplode = [0.2, 0, 0, 0]
plt.pie(y, labels=mylabels,
startangle=90,explode=myexplode)
plt.show()
Maybe you want one of the wedges to stand out? The explode parameter allows you to do that.
The explode parameter, if specified, and not None, must be an array with one value for each wedge
Shadow
import matplotlib.pyplot as plt
import numpy as np
x = np.array([35,25,25,15])
mylabels = ["Apples", "Bananas", "Cherries",
"Dates"]
myexplode = [0.2, 0, 0, 0]
plt.pie(y, labels=mylabels, startangle=90,
explode=myexplode, shadow=True)
plt.show()
Add a shadow to the pie chart by setting the shadows parameter to True:
Colors
import matplotlib.pyplot as plt
import numpy as np
x = np.array([35,25,25,15])
mylabels = ["Apples", "Bananas", "Cherries",
"Dates"]
mycolors = ["black", "hotpink", "b", "#4CAF50"]
myexplode = [0.2, 0, 0, 0]
plt.pie(y, labels=mylabels, startangle=90,
explode=myexplode, colors= mycolors)
plt.show()
You can set the color of each wedge with the colors parameter.
Legend
import matplotlib.pyplot as plt
import numpy as np
x = np.array([35,25,25,15])
mylabels = ["Apples", "Bananas", "Cherries",
"Dates"]
plt.pie(y, labels=mylabels)
plt.legend()
To add a list of explanation for each wedge, use the legend() function:
plt.show()
Legend With Header
import matplotlib.pyplot as plt
import numpy as np
x = np.array([35,25,25,15])
mylabels = ["Apples", "Bananas", "Cherries",
"Dates"]
plt.pie(y, labels=mylabels)
plt.legend(title=‘Four Fruits:’)
To add a header to the legend, add the title parameter to the legend function.
plt.show()

More Related Content

PPTX
MatplotLib.pptx
PPTX
Unit III for data science engineering.pptx
PPTX
2. Python Library Matplotlibmmmmmmmm.pptx
PPTX
Unit3-v1-Plotting and Visualization.pptx
PDF
12-IP.pdf
PPTX
Matplotlib yayyyyyyyyyyyyyin Python.pptx
PPTX
a9bf73_Introduction to Matplotlib01.pptx
PDF
UNit-III. part 2.pdf
MatplotLib.pptx
Unit III for data science engineering.pptx
2. Python Library Matplotlibmmmmmmmm.pptx
Unit3-v1-Plotting and Visualization.pptx
12-IP.pdf
Matplotlib yayyyyyyyyyyyyyin Python.pptx
a9bf73_Introduction to Matplotlib01.pptx
UNit-III. part 2.pdf

Similar to matplotlib.pptxdsfdsfdsfdsdsfdsdfdsfsdf cvvf (20)

PPTX
Python Visualization API Primersubplots
PPTX
Introduction to matplotlib
PPTX
Visualization and Matplotlib using Python.pptx
PDF
711118749-FDS-UNIT-5-PPT.pdf is used to the engineering students
PPTX
Introduction to Pylab and Matploitlib.
PPTX
Python Pyplot Class XII
PPTX
UNIT-5-II IT-DATA VISUALIZATION TECHNIQUES
DOCX
Data visualization using py plot part i
PPTX
Matplotlib.pptx for data analysis and visualization
PPTX
BASIC OF PYTHON MATPLOTLIB USED IN ARTIFICIAL INTELLIGENCE AND ML
PDF
Matplotlib 簡介與使用
PDF
The matplotlib Library
PPTX
Python_Matplotlib_13_Slides_With_Diagrams.pptx
PDF
Use the Matplotlib, Luke @ PyCon Taiwan 2012
PDF
Python matplotlib cheat_sheet
PPTX
Matplot Lib Practicals artificial intelligence.pptx
PDF
UNIT-2.data exploration and visualization
PPTX
Matplotlib_Presentation jk jdjklskncncsjkk
PDF
Gráficas en python
PPTX
Matplotlib - Python Plotting Library Description
Python Visualization API Primersubplots
Introduction to matplotlib
Visualization and Matplotlib using Python.pptx
711118749-FDS-UNIT-5-PPT.pdf is used to the engineering students
Introduction to Pylab and Matploitlib.
Python Pyplot Class XII
UNIT-5-II IT-DATA VISUALIZATION TECHNIQUES
Data visualization using py plot part i
Matplotlib.pptx for data analysis and visualization
BASIC OF PYTHON MATPLOTLIB USED IN ARTIFICIAL INTELLIGENCE AND ML
Matplotlib 簡介與使用
The matplotlib Library
Python_Matplotlib_13_Slides_With_Diagrams.pptx
Use the Matplotlib, Luke @ PyCon Taiwan 2012
Python matplotlib cheat_sheet
Matplot Lib Practicals artificial intelligence.pptx
UNIT-2.data exploration and visualization
Matplotlib_Presentation jk jdjklskncncsjkk
Gráficas en python
Matplotlib - Python Plotting Library Description
Ad

More from zmulani8 (20)

PPTX
Chapter-9-MySQL.pptxxzrtyryrydfdsfdsfdsrter
PPTX
unit 2 Mastering-State-Management-in-React.pptx
PPTX
Lect 4.pptxdsdsdsdfgxgf xzffss sdsdsffff
PPTX
sql functions.pptxghghghghghghghbvnbghjj
PPTX
session_2_sqlpptxfhfhfhfdhfdhkkfdhfdhfdh
PPTX
unit_1_foss_2.pptxbfhfdhfgsdgtsdegtsdtetg
PPTX
unit_1_spring_1.pptxfgfgggjffgggddddgggg
PPTX
spring aop.pptx aspt oreinted programmin
PPTX
Some more Concepts of DOT cvcvcvNET.pptx
PPTX
DOT NET Framework.pptxdsfdsfdsfsdfdsfdsfdsf
PDF
ipsec.pdfgvdgvdgdgdgddgdgdgdgdgdgdgdgdgd
PPT
unit 2 intr to phy layer part 1.pptcvcvcv
PPT
JSP 1.pptdfdfdfdsfdsfdsfdsfdsgdgdgdgdgdd
PPTX
swing_compo.pptxsfdsfffdfdfdfdgwrwrwwtry
PPT
introduction.pptdasdasdadasdasdsddsdsads
PPTX
PE introd.pptxdsdsdsdasdsdsddadqwdqwdqwdqw
PPTX
Pre_requisties of ML Lect 1.pptxvcbvcbvcbvcb
PPTX
introduction TO DS 1.pptxvbvcbvcbvcbvcbvcb
PPTX
IANSunit 1_cryptography_2.pptxv xvxvxvxv
PPT
ch03.pptvxcvxcvxcvxcvxcvxcvcxvdsgedgeeee
Chapter-9-MySQL.pptxxzrtyryrydfdsfdsfdsrter
unit 2 Mastering-State-Management-in-React.pptx
Lect 4.pptxdsdsdsdfgxgf xzffss sdsdsffff
sql functions.pptxghghghghghghghbvnbghjj
session_2_sqlpptxfhfhfhfdhfdhkkfdhfdhfdh
unit_1_foss_2.pptxbfhfdhfgsdgtsdegtsdtetg
unit_1_spring_1.pptxfgfgggjffgggddddgggg
spring aop.pptx aspt oreinted programmin
Some more Concepts of DOT cvcvcvNET.pptx
DOT NET Framework.pptxdsfdsfdsfsdfdsfdsfdsf
ipsec.pdfgvdgvdgdgdgddgdgdgdgdgdgdgdgdgd
unit 2 intr to phy layer part 1.pptcvcvcv
JSP 1.pptdfdfdfdsfdsfdsfdsfdsgdgdgdgdgdd
swing_compo.pptxsfdsfffdfdfdfdgwrwrwwtry
introduction.pptdasdasdadasdasdsddsdsads
PE introd.pptxdsdsdsdasdsdsddadqwdqwdqwdqw
Pre_requisties of ML Lect 1.pptxvcbvcbvcbvcb
introduction TO DS 1.pptxvbvcbvcbvcbvcbvcb
IANSunit 1_cryptography_2.pptxv xvxvxvxv
ch03.pptvxcvxcvxcvxcvxcvxcvcxvdsgedgeeee
Ad

Recently uploaded (20)

PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Construction Project Organization Group 2.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPT
Project quality management in manufacturing
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Digital Logic Computer Design lecture notes
PPTX
UNIT 4 Total Quality Management .pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Construction Project Organization Group 2.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Project quality management in manufacturing
OOP with Java - Java Introduction (Basics)
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Operating System & Kernel Study Guide-1 - converted.pdf
R24 SURVEYING LAB MANUAL for civil enggi
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
CH1 Production IntroductoryConcepts.pptx
Lecture Notes Electrical Wiring System Components
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
Digital Logic Computer Design lecture notes
UNIT 4 Total Quality Management .pptx

matplotlib.pptxdsfdsfdsfdsdsfdsdfdsfsdf cvvf

  • 2. Matplotlib Matplotlib is a low level graph plotting library in python that serves as a visualization utility.
  • 3. Matplotlib Created By- John D. Hunter Developed- 2002 Release- 2003
  • 4. How to install Matplotlib in windows py -m pip install matplotlib  Open Command Prompt in windows  Than set user account  And run follow command ( When computer is connected to internet )
  • 5. How to use Matplotlib import matplotlib as mt
  • 6. Check Version import matplotlib as mt Print(mt. version ) Output: 4.0
  • 7. Matplotlib Pyplot import matplotlib.pyplot as plt Most of the Matplotlib utilities lies under the pyplot submodule, and are usually imported under the plt alias:
  • 8. Plotting x and y points  The plot() function is used to draw points (markers) in a diagram.  By default, the plot() function draws a line from point to point.  The function takes parameters for specifying points in the diagram.  Parameter 1 is an array containing the points on the x-axis (horizontal axis).  Parameter 2 is an array containing the points on the y-axis (vertical axis)..
  • 9. Plotting Line import matplotlib.pyplot as plt import numpy as np x= np.array([0, 6]) y = np.array([0, 250]) Draw a line in a diagram from position (0,0) to position (6,250): plt.plot(x, y) plt.show()
  • 10. Plotting without Line import matplotlib.pyplot as plt import numpy as np x= np.array([0, 6]) y = np.array([0, 250]) To plot only the markers, you can use shortcut string notation parameter 'o', which means 'rings'. plt.plot(x, y, ‘o’) plt.show()
  • 11. Multiple Points import matplotlib.pyplot as plt import numpy as np x= np.array([1, 2,6,8]) y = np.array([3,8,1,10]) plt.plot(x, y) plt.show()
  • 12. Default X-Axis import matplotlib.pyplot as plt import numpy as np y = np.array([3,8,1,10,5,7]) If we do not specify the points in the x-axis, they will get the default values 0, 1, 2, 3, (etc. depending on the length of the y-points. plt.plot( y) plt.show()
  • 13. Matplotlib Markers import matplotlib.pyplot as plt import numpy as np y = np.array([3,8,1,10]) You can use the keyword argument marker to emphasize each point with a specified marker: plt.plot( y, marker=‘o’) plt.show()
  • 14. Markers Reference Marker Description 'o' Circle '*' Star '.' Point 'x' X 'X' X (filled) '+' Plus 'P' Plus (filled) 's' Square 'D' Diamond Marker Description 'd' Diamond (thin) 'p' Pentagon 'H' Hexagon 'h' Hexagon 'v' Triangle Down '^' Triangle Up '<' Triangle Left '>' Triangle Right
  • 15. Format Strings fmt import matplotlib.pyplot as plt import numpy as np y = np.array([3,8,1,10]) plt.plot( y, ‘o:r’) plt.show() This parameter is also called fmt, and is written with this syntax: marker|line|color
  • 16. Line Reference Line Syntax Description ‘-' Solid Line ‘:' Dotted Line ‘--' Dashed Line ‘-.' Dashed/ Dotted Line
  • 17. Color Reference Color Syntax Description ‘r' Red ‘g' Green ‘b' Blue ‘c' Cyan ‘m’ Magenta ‘y’ Yellow ‘k’ Black ‘w’ White
  • 18. Markers Size import matplotlib.pyplot as plt import numpy as np y = np.array([3,8,1,10]) plt.plot( y, marker= ‘o’, ms= 20) You can use the keyword argument markersize or the shorter version, ms to set the size of the markers: plt.show()
  • 19. Markers Edge Size import matplotlib.pyplot as plt import numpy as np y = np.array([3,8,1,10]) You can use the keyword argument markeredgecolor or the shorter mec to set the color of the edge of the markers: plt.plot( y, mec=‘r’) plt.show() marker= ‘o’, ms= 20,
  • 20. Markers Face Size import matplotlib.pyplot as plt import numpy as np y = np.array([3,8,1,10]) You can use the keyword argument markerfacecolor or the shorter mfc to set the color of the edge of the markers: plt.plot( y, mfc=‘r’) plt.show() marker= ‘o’, ms= 20,
  • 21. Linestyle Argument import matplotlib.pyplot as plt import numpy as np y = np.array([3,8,1,10]) plt.plot( y, linestyle=‘dotted’) You can use the keyword argument linestyle, or shorter ls, to change the style of the plotted line plt.show()
  • 22. Line Reference ls Syntax linestyle Syntax ‘-' ‘solid’ ‘:' ‘dotted’ ‘--' ‘dashed’ ‘-.' ‘dashdot’
  • 23. Line Color Argument import matplotlib.pyplot as plt import numpy as np y = np.array([3,8,1,10]) plt.plot( y, color=‘r’) You can use the keyword argument color or the shorter c to set the color of the line: plt.show()
  • 24. Line width Argument import matplotlib.pyplot as plt import numpy as np y = np.array([3,8,1,10]) plt.plot( y, linewidth=’20.6’) You can use the keyword argument linewidth or the shorter lw to change the width of the line plt.show()
  • 25. Multiple width Argument import matplotlib.pyplot as plt import numpy as np x1 = np.array([0,1,2,3]) y1 = np.array([3,8,1,10]) x2 = np.array([0,1,2,3]) y2 = np.array([6,2,7,11]) plt.plot( x1,y1,x2,y2) You can use the keyword argument linewidth or the shorter lw to change the width of the line plt.show()
  • 26. Create Labels import matplotlib.pyplot as plt import numpy as np x = np.array([0,1,2,3,4,5]) y = np.array([0,8,12,20,26,38]) plt.plot(x,y) plt.xlabel(“Overs’) With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x- and y-axis. plt.ylabel(“Runs’) plt.show()
  • 27. Create Title matplotlib.pyplot as plt import numpy as np x = np.array([0,1,2,3,4,5]) y = np.array([0,8,12,20,26,3 8]) plt.plot(x,y) plt.xlabel(“Overs’) plt.ylabel(“Runs’) With Pyplot, you can use the title() function to set a title for the plot. Sport Data Plt.title(“Sport Data”) plt.show()
  • 28. Set Font Properties for Title and Labels You can use the fontdict parameter in xlabel(), ylabel(), and title() to set font properties for the title and labels. import matplotlib.pyplot as plt import numpy as np x = np.array([0,1,2,3,4,5]) y = np.array([0,8,12,20,26,38]) font1 = {'family':'serif','color':'blue','size':20} font2 = {'family':'serif','color':'darkred','size':15} plt.plot(x,y) plt.xlabel(“Overs’,fontdict=font2) plt.ylabel(“Runs’ ,fontdict=font2) Plt.title(“Sport Data” ,fontdict=font1) plt.show()
  • 29. Set Font Properties for Title and Labels
  • 30. Position the Title import matplotlib.pyplot as plt import numpy as np x = np.array([0,1,2,3,4,5]) y = np.array([0,8,12,20,26,38]) plt.plot(x,y) plt.xlabel(“Overs’) plt.ylabel(“Runs’) You can use the loc parameter in title() to position the title. Legal values are: 'left', 'right', and 'center'. Default value is 'center'. Plt.title(“Sport Data”, loc=‘left’) plt.show()
  • 31. Add Grid Lines to a Plot import matplotlib.pyplot as plt import numpy as np x = np.array([0,1,2,3,4,5]) y = np.array([0,8,12,20,26,38]) plt.plot(x,y) plt.xlabel(“Overs’) plt.ylabel(“Runs’) Plt.title(“Sport Data”, loc=‘left’) With Pyplot, you can use the grid() function to add grid lines to the plot. Plt.grid() plt.show()
  • 32. Display Multiple Plots With the subplot() function you can draw multiple plots in one figure:  The subplot() function takes three arguments that describes the layout of the figure.  The layout is organized in rows and columns, which are represented by the first and second argument.  The third argument represents the index of the current plot.
  • 33. Display Multiple Plots import matplotlib.pyplot as plt import numpy as np #plot 1 x = np.array([0,1,2,3]) y = np.array([3,8,1,10]) plt.subplot(1,2,1) plt.plot(x,y) #plot 2 x = np.array([0,1,2,3]) y = np.array([10,20,30,40]) plt.subplot(1,2,2) plt.plot(x,y) plt.show()
  • 34. Display Multiple Plots import matplotlib.pyplot as plt import numpy as np #plot 1 x = np.array([0,1,2,3]) y = np.array([3,8,1,10]) plt.subplot(2,1,1) plt.plot(x,y) #plot 2 x = np.array([0,1,2,3]) y = np.array([10,20,30,40]) plt.subplot(2,1,2) plt.plot(x,y) plt.show()
  • 35. Super Title import matplotlib.pyplot as plt import numpy as np #plot 1 x = np.array([0,1,2,3]) y = np.array([3,8,1,10]) plt.subplot(2,1,1) plt.plot(x,y) #plot 2 x = np.array([0,1,2,3]) y = np.array([10,20,30,40]) plt.subplot(2,2,1) plt.plot(x,y) You can add a title to the entire figure with the suptitle() function: plt.suptitle(“My Data”) plt.show()
  • 36. Creating Scatter Plots  With Pyplot, you can use the scatter() function to draw a scatter plot.  The scatter() function plots one dot for each observation. It needs two arrays of the same length, one for the values of the x-axis, and one for values on the y-axis:
  • 37. Scatter Plots import matplotlib.pyplot as plt import numpy as np x = np.array([0,1,2,3,4,5]) y = np.array([0,8,12,20,26,38]) plt.scatter(x,y) plt.show()
  • 38. Compare Plots import matplotlib.pyplot as plt import numpy as np x = np.array([0,1,2,3,4,5]) y = np.array([0,2,8,1,14,7]) plt.scatter(x,y,color='red') x = np.array([12,6,8,11,8,3]) y = np.array([5,6,3,7,17,19]) plt.scatter(x,y,color='green ') plt.show()
  • 39. Color each dots import matplotlib.pyplot as plt import numpy as np x = np.array([0,1,2,3,4,5]) y = np.array([0,2,8,1,14,7]) mycolor=['red','green','purple','lime','aqua',' yellow'] plt.scatter(x,y,color= mycolor ) plt.show()
  • 40. Size import matplotlib.pyplot as plt import numpy as np x = np.array([0,1,2,3,4,5]) y = np.array([0,2,8,1,14,7]) mycolor=['red','green','purple','lime','aqua',' yellow'] size=[10,60,120,80,20,190] plt.scatter(x,y,color= mycolor, s=size ) You can change the size of the dots with the s argument. plt.show()
  • 41. Alpha import matplotlib.pyplot as plt import numpy as np x = np.array([0,1,2,3,4,5]) y = np.array([0,2,8,1,14,7]) mycolor=['red','green','purple','lime','aqua',' yellow'] size=[10,60,120,80,20,190] plt.scatter(x,y,color= mycolor, s=size, alpha=0.3 ) You can adjust the transparency of the dots with the alpha argument. plt.show()
  • 42. Create Bar import matplotlib.pyplot as plt import numpy as np x = np.array([‘A’,’B’,’C’,’D’]) y = np.array([3,8,1,10]) plt.bar(x,y) With Pyplot, you can use the bar() function to draw bar graphs: plt.show()
  • 43. Create Horizontal Bar import matplotlib.pyplot as plt import numpy as np x = np.array([‘A’,’B’,’C’,’D’]) y = np.array([3,8,1,10]) plt.barh(x,y) If you want the bars to be displayed horizontally instead of vertically, use the barh() function: plt.show()
  • 44. Bar Width import matplotlib.pyplot as plt import numpy as np x = np.array([‘A’,’B’,’C’,’D’]) y = np.array([3,8,1,10]) The bar() takes the keyword argument width to set the width of the bars: plt.barh(x,y,width=0.1) plt.show()
  • 45. Histogram A histogram is a graph showing frequency distributions. It is a graph showing the number of observations within each given interval. In Matplotlib, we use the hist() function to create histograms. The hist() function will read the array and produce a histogram:
  • 46. Histogram import matplotlib.pyplot as plt import numpy as np x = np.random.normal(170,10,250) plt.hist(x) plt.show()
  • 47. Creating Pie Charts import matplotlib.pyplot as plt import numpy as np x = np.array([35,25,25,15]) plt.pie(y) plt.show() With Pyplot, you can use the pie() function to draw pie charts:
  • 48. Labels import matplotlib.pyplot as plt import numpy as np x = np.array([35,25,25,15]) mylabels = ["Apples", "Bananas", "Cherries", "Dates"] plt.pie(y, labels=mylabels) Add labels to the pie chart with the label parameter. The label parameter must be an array with one label for each wedge: plt.show()
  • 49. Start Angle import matplotlib.pyplot as plt import numpy as np x = np.array([35,25,25,15]) mylabels = ["Apples", "Bananas", "Cherries", "Dates"] plt.pie(y, labels=mylabels,startangle=90) As mentioned the default start angle is at the x-axis, but you can change the start angle by specifying a startangle parameter. plt.show()
  • 50. Explode import matplotlib.pyplot as plt import numpy as np x = np.array([35,25,25,15]) mylabels = ["Apples", "Bananas", "Cherries", "Dates"] myexplode = [0.2, 0, 0, 0] plt.pie(y, labels=mylabels, startangle=90,explode=myexplode) plt.show() Maybe you want one of the wedges to stand out? The explode parameter allows you to do that. The explode parameter, if specified, and not None, must be an array with one value for each wedge
  • 51. Shadow import matplotlib.pyplot as plt import numpy as np x = np.array([35,25,25,15]) mylabels = ["Apples", "Bananas", "Cherries", "Dates"] myexplode = [0.2, 0, 0, 0] plt.pie(y, labels=mylabels, startangle=90, explode=myexplode, shadow=True) plt.show() Add a shadow to the pie chart by setting the shadows parameter to True:
  • 52. Colors import matplotlib.pyplot as plt import numpy as np x = np.array([35,25,25,15]) mylabels = ["Apples", "Bananas", "Cherries", "Dates"] mycolors = ["black", "hotpink", "b", "#4CAF50"] myexplode = [0.2, 0, 0, 0] plt.pie(y, labels=mylabels, startangle=90, explode=myexplode, colors= mycolors) plt.show() You can set the color of each wedge with the colors parameter.
  • 53. Legend import matplotlib.pyplot as plt import numpy as np x = np.array([35,25,25,15]) mylabels = ["Apples", "Bananas", "Cherries", "Dates"] plt.pie(y, labels=mylabels) plt.legend() To add a list of explanation for each wedge, use the legend() function: plt.show()
  • 54. Legend With Header import matplotlib.pyplot as plt import numpy as np x = np.array([35,25,25,15]) mylabels = ["Apples", "Bananas", "Cherries", "Dates"] plt.pie(y, labels=mylabels) plt.legend(title=‘Four Fruits:’) To add a header to the legend, add the title parameter to the legend function. plt.show()