SlideShare a Scribd company logo
1 of 54
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])
plt.plot(x, y)
plt.show()
Draw a line in a diagram from position (0,0) to position (6,250):
Plotting without Line
import matplotlib.pyplot as plt
import numpy as np
x= np.array([0, 6])
y = np.array([0, 250])
plt.plot(x, y, ‘o’)
plt.show()
To plot only the markers, you can use shortcut string notation parameter 'o', which
means 'rings'.
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])
plt.plot( y)
plt.show()
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.
Matplotlib Markers
import matplotlib.pyplot as plt
import numpy as np
y = np.array([3,8,1,10])
plt.plot( y, marker=‘o’)
plt.show()
You can use the keyword argument marker to emphasize each point with a specified
marker:
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)
plt.show()
You can use the keyword argument markersize or the shorter version, ms to set the
size of the markers:
Markers Edge Size
import matplotlib.pyplot as plt
import numpy as np
y = np.array([3,8,1,10])
plt.plot( y, marker= ‘o’, ms= 20,
mec=‘r’)
plt.show()
You can use the keyword argument markeredgecolor or the shorter mec to set the
color of the edge of the markers:
Markers Face Size
import matplotlib.pyplot as plt
import numpy as np
y = np.array([3,8,1,10])
plt.plot( y, marker= ‘o’, ms= 20,
mfc=‘r’)
plt.show()
You can use the keyword argument markerfacecolor or the shorter mfc to set the
color of the edge of the markers:
Linestyle Argument
import matplotlib.pyplot as plt
import numpy as np
y = np.array([3,8,1,10])
plt.plot( y, linestyle=‘dotted’)
plt.show()
You can use the keyword argument linestyle, or shorter ls, to change the style of the
plotted line
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’)
plt.show()
You can use the keyword argument color or the shorter c to set the color of the line:
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’)
plt.show()
You can use the keyword argument linewidth or the shorter lw to change the width of
the line
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)
plt.show()
You can use the keyword argument linewidth or the shorter lw to change the width of
the line
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’)
plt.ylabel(“Runs’)
plt.show()
With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x-
and y-axis.
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,38])
plt.plot(x,y)
plt.xlabel(“Overs’)
plt.ylabel(“Runs’)
Plt.title(“Sport Data”)
plt.show()
With Pyplot, you can use the title() function to set a title for the plot.
Sport Data
Set Font Properties for 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()
You can use the fontdict parameter in xlabel(), ylabel(), and title() to set font
properties for the title and labels.
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’)
Plt.title(“Sport Data”, loc=‘left’)
plt.show()
You can use the loc parameter in title() to position the title.
Legal values are: 'left', 'right', and 'center'. Default value is 'center'.
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’)
Plt.grid()
plt.show()
With Pyplot, you can use the grid() function to add grid lines to the plot.
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)
plt.suptitle(“My Data”)
plt.show()
You can add a title to the entire figure with the suptitle() function:
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 )
plt.show()
You can change the size of the dots with the s argument.
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 )
plt.show()
You can adjust the transparency of the dots with the alpha argument.
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)
plt.show()
With Pyplot, you can use the bar() function to draw bar graphs:
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)
plt.show()
If you want the bars to be displayed horizontally instead of vertically, use the barh()
function:
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])
plt.barh(x,y,width=0.1)
plt.show()
The bar() takes the keyword argument width to set the width of the bars:
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)
plt.show()
Add labels to the pie chart with the label parameter.
The label parameter must be an array with one label for each wedge:
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)
plt.show()
As mentioned the default start angle is at the x-axis, but you can change the start
angle by specifying a startangle parameter.
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()
plt.show()
To add a list of explanation for each wedge, use the legend() function:
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:’)
plt.show()
To add a header to the legend, add the title parameter to the legend function.

More Related Content

What's hot

What's hot (20)

Python pandas Library
Python pandas LibraryPython pandas Library
Python pandas Library
 
Data Analysis and Visualization using Python
Data Analysis and Visualization using PythonData Analysis and Visualization using Python
Data Analysis and Visualization using Python
 
pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data Analysis
 
Matplotlib
MatplotlibMatplotlib
Matplotlib
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
 
Introduction to numpy Session 1
Introduction to numpy Session 1Introduction to numpy Session 1
Introduction to numpy Session 1
 
Python Seaborn Data Visualization
Python Seaborn Data Visualization Python Seaborn Data Visualization
Python Seaborn Data Visualization
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
Seaborn.pptx
Seaborn.pptxSeaborn.pptx
Seaborn.pptx
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
pandas: Powerful data analysis tools for Python
pandas: Powerful data analysis tools for Pythonpandas: Powerful data analysis tools for Python
pandas: Powerful data analysis tools for Python
 
Data Analysis with Python Pandas
Data Analysis with Python PandasData Analysis with Python Pandas
Data Analysis with Python Pandas
 
Data visualization in Python
Data visualization in PythonData visualization in Python
Data visualization in Python
 
Introduction to numpy
Introduction to numpyIntroduction to numpy
Introduction to numpy
 
Data Analysis in Python
Data Analysis in PythonData Analysis in Python
Data Analysis in Python
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...
Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...
Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
 
Data Visualization in Python
Data Visualization in PythonData Visualization in Python
Data Visualization in Python
 
Python Libraries and Modules
Python Libraries and ModulesPython Libraries and Modules
Python Libraries and Modules
 

Similar to MatplotLib.pptx

matplotlib-installatin-interactive-contour-example-guide
matplotlib-installatin-interactive-contour-example-guidematplotlib-installatin-interactive-contour-example-guide
matplotlib-installatin-interactive-contour-example-guide
Arulalan T
 

Similar to MatplotLib.pptx (20)

Py lecture5 python plots
Py lecture5 python plotsPy lecture5 python plots
Py lecture5 python plots
 
UNit-III. part 2.pdf
UNit-III. part 2.pdfUNit-III. part 2.pdf
UNit-III. part 2.pdf
 
16. Data VIsualization using PyPlot.pdf
16. Data VIsualization using PyPlot.pdf16. Data VIsualization using PyPlot.pdf
16. Data VIsualization using PyPlot.pdf
 
Basic Analysis using Python
Basic Analysis using PythonBasic Analysis using Python
Basic Analysis using Python
 
12-IP.pdf
12-IP.pdf12-IP.pdf
12-IP.pdf
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdf
 
Data Visualization 2020_21
Data Visualization 2020_21Data Visualization 2020_21
Data Visualization 2020_21
 
matplotlib-installatin-interactive-contour-example-guide
matplotlib-installatin-interactive-contour-example-guidematplotlib-installatin-interactive-contour-example-guide
matplotlib-installatin-interactive-contour-example-guide
 
Class 8b: Numpy & Matplotlib
Class 8b: Numpy & MatplotlibClass 8b: Numpy & Matplotlib
Class 8b: Numpy & Matplotlib
 
Python High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.pptPython High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.ppt
 
Matlab1
Matlab1Matlab1
Matlab1
 
Presentation: Plotting Systems in R
Presentation: Plotting Systems in RPresentation: Plotting Systems in R
Presentation: Plotting Systems in R
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdf
 
R scatter plots
R scatter plotsR scatter plots
R scatter plots
 
Use the Matplotlib, Luke @ PyCon Taiwan 2012
Use the Matplotlib, Luke @ PyCon Taiwan 2012Use the Matplotlib, Luke @ PyCon Taiwan 2012
Use the Matplotlib, Luke @ PyCon Taiwan 2012
 
NUMPY-2.pptx
NUMPY-2.pptxNUMPY-2.pptx
NUMPY-2.pptx
 
Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib. Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib.
 
Python for Data Science
Python for Data SciencePython for Data Science
Python for Data Science
 
statistical computation using R- an intro..
statistical computation using R- an intro..statistical computation using R- an intro..
statistical computation using R- an intro..
 
De-Cluttering-ML | TechWeekends
De-Cluttering-ML | TechWeekendsDe-Cluttering-ML | TechWeekends
De-Cluttering-ML | TechWeekends
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
 

MatplotLib.pptx

  • 1.
  • 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]) plt.plot(x, y) plt.show() Draw a line in a diagram from position (0,0) to position (6,250):
  • 10. Plotting without Line import matplotlib.pyplot as plt import numpy as np x= np.array([0, 6]) y = np.array([0, 250]) plt.plot(x, y, ‘o’) plt.show() To plot only the markers, you can use shortcut string notation parameter 'o', which means 'rings'.
  • 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]) plt.plot( y) plt.show() 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.
  • 13. Matplotlib Markers import matplotlib.pyplot as plt import numpy as np y = np.array([3,8,1,10]) plt.plot( y, marker=‘o’) plt.show() You can use the keyword argument marker to emphasize each point with a specified marker:
  • 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) plt.show() You can use the keyword argument markersize or the shorter version, ms to set the size of the markers:
  • 19. Markers Edge Size import matplotlib.pyplot as plt import numpy as np y = np.array([3,8,1,10]) plt.plot( y, marker= ‘o’, ms= 20, mec=‘r’) plt.show() You can use the keyword argument markeredgecolor or the shorter mec to set the color of the edge of the markers:
  • 20. Markers Face Size import matplotlib.pyplot as plt import numpy as np y = np.array([3,8,1,10]) plt.plot( y, marker= ‘o’, ms= 20, mfc=‘r’) plt.show() You can use the keyword argument markerfacecolor or the shorter mfc to set the color of the edge of the markers:
  • 21. Linestyle Argument import matplotlib.pyplot as plt import numpy as np y = np.array([3,8,1,10]) plt.plot( y, linestyle=‘dotted’) plt.show() You can use the keyword argument linestyle, or shorter ls, to change the style of the plotted line
  • 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’) plt.show() You can use the keyword argument color or the shorter c to set the color of the line:
  • 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’) plt.show() You can use the keyword argument linewidth or the shorter lw to change the width of the line
  • 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) plt.show() You can use the keyword argument linewidth or the shorter lw to change the width of the line
  • 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’) plt.ylabel(“Runs’) plt.show() With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x- and y-axis.
  • 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,38]) plt.plot(x,y) plt.xlabel(“Overs’) plt.ylabel(“Runs’) Plt.title(“Sport Data”) plt.show() With Pyplot, you can use the title() function to set a title for the plot. Sport Data
  • 28. Set Font Properties for 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() You can use the fontdict parameter in xlabel(), ylabel(), and title() to set font properties for the title and labels.
  • 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’) Plt.title(“Sport Data”, loc=‘left’) plt.show() You can use the loc parameter in title() to position the title. Legal values are: 'left', 'right', and 'center'. Default value is 'center'.
  • 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’) Plt.grid() plt.show() With Pyplot, you can use the grid() function to add grid lines to the plot.
  • 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) plt.suptitle(“My Data”) plt.show() You can add a title to the entire figure with the suptitle() function:
  • 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 ) plt.show() You can change the size of the dots with the s argument.
  • 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 ) plt.show() You can adjust the transparency of the dots with the alpha argument.
  • 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) plt.show() With Pyplot, you can use the bar() function to draw bar graphs:
  • 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) plt.show() If you want the bars to be displayed horizontally instead of vertically, use the barh() function:
  • 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]) plt.barh(x,y,width=0.1) plt.show() The bar() takes the keyword argument width to set the width of the bars:
  • 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) plt.show() Add labels to the pie chart with the label parameter. The label parameter must be an array with one label for each wedge:
  • 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) plt.show() As mentioned the default start angle is at the x-axis, but you can change the start angle by specifying a startangle parameter.
  • 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() plt.show() To add a list of explanation for each wedge, use the legend() function:
  • 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:’) plt.show() To add a header to the legend, add the title parameter to the legend function.