0% found this document useful (0 votes)
2 views32 pages

Mat Plot Lib

Data analytics

Uploaded by

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

Mat Plot Lib

Data analytics

Uploaded by

poojithachougani
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

UNIT-I

PYTHON-MATPLOTLIB

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Q1: Introduction to Matplotlib:
Matplotlib is the most popular multi-platform MATPLOTLIB library for python which gives
control over every aspect of a figure. It was designed to give the end user a similar feeling like
01
MATLAB‟s . 08 Middle Level
Language 02
The multidimensional MATPLOTLIB built on NumPy arrays
Memory Structured
Management Language
Installation:
Matplotlib and its dependency packages are installed , when we install the Matplotlib by using
Speed 03
following pip command. Rich
07 Library

pip install matplotlib


n ters 04
Po i
[Link] is a collection of command style functions. Extensible
Recursion
Each pyplot function makes some change to a figure. For example, a function creates a figure, a
05
plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Introduction to Matplotlib:
Matplotlib has a comprehensive library for creating static, animated, and interactive
visualizations in Python.
01
08 3dMiddle
 Easy for 2-dimensional plotting and limited plotting
Levelwith Python
02
Language
Memory Structured
 It can also be used with graphics toolkits like PyQt and wxPython
Management Language

 Matplotlib module was first written by John D. Hunter in 2003


Speed 03
 The current stable version is 3.5.2 released in May 2022. Rich
07 Library

The official page of Matplotlib is n ters 04


Po i
Extensible
Recursion
[Link]
05
You can use this page for official installation instructions and various documentation
links.
MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Another one of the most important sections on this page is gallery section

[Link]
01
08 Middle Level
Language
It shows all the kind of plots/figures that Matplotlib is capable
Memory
Management
of creating for you. You
Structured
Language
can select anyone of those, and it takes you the example page having the figure and very
well documented code. Speed 03
Rich
07 Library

Another important page is


n ters 04
[Link]
Po i
Extensible
Recursion
It shows the documentation functions in it.
05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
1: Basic Plots using Matplotlib
1. Area Plots or area chart
2. Line plots 01
3. Bar plots or Bar charts 08 Middle Level
Language 02
4. Histograms Memory Structured
Management Language

2. Specialized Visualization Tools using Matplotlib


1. Pie charts Speed 03
Rich
2. Box plots 07 Library

3. Scatter Plots
nte
rs 04
4. Bubble plots P o i
Extensible
3: Advanced Visualization Tools using Matplotlib
Recursion
1. Waffle Charts
05
2. Word Clouds

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
1. Area Plot (or) Area chart:

An area chart is really similar to a line chart, except that the area between the x axis and
01
the line is filled in with color or shading. 08 Middle Level
Language 02
Memory Structured
Management Language
There are 2 main ways to build an area chart with Matplotlib. In both case it requires 2
numeric vectors of values as input and use any one of the following function.
Speed 03
Rich
07 Library
1. fill_between() function
2. stackplot() function n ters 04
Po i
Extensible
Recursion

05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Example 1: to draw the area plot by using fill_between() function.

import [Link] as plt


01
# Create data 08 Middle Level
Language 02
x=[1,2,3,4,6] Memory Structured
Management Language
y=[1,4,5,8,4]
# Area plot Speed 03
Rich
plt.fill_between(x, y) 07 Library

[Link]() n ters 04
Po i
Extensible
Recursion

05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Example 1: to draw the area plot by using stackplot() function.

import [Link] as plt


01
# Create data 08 Middle Level
Language 02
# x=range(1,6) Memory Structured
Management Language
x=[1,2,3,4,5]
y=[1,4,5,8,5] Speed 03
# Area plot Rich
07 Library
[Link](x,y)
ters 04
[Link]() Po i n
Extensible
Recursion
Output:
05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Line Plot (or) Line chart:

01
[Link](x,y) 08 Middle Level
Language 02
[Link]. title() Memory
Management
Structured
Language

[Link]. xlabel()
Speed 03
[Link]. ylabel() 07
Rich
Library

[Link]() 04
i n ters
Po Extensible
Recursion

05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Example 1: to draw the line plot without labels by using plot() function.

01
import [Link] as plt 08 Middle Level
Language 02
Memory Structured
plt. plot ([1, 2, 3], [4, 5, 1]) Management Language

[Link] () Speed 03
Rich
07 Library

n ters 04
Po i
Extensible
Recursion

05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Example 2: to draw the line plot with labels by using plot() function.

01
x=[1,2,4,8,9] 08 Middle Level
Language 02
Memory Structured
y=[1,3,6,6,4] Management Language

[Link](x,y)
Speed 03
[Link]('sample line graph') Rich
07 Library

[Link]('x values')
n ters 04
Po i
[Link]('y values') Extensible
Recursion
[Link]()
05

PYTHON PROGRAMMING UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Formatting the style of the plot:
Example 3 :
import numpy as np
import [Link] as plt 01
08 Middle Level
x = [Link] (1, 11) Language 02
Memory Structured
y=2*x+5 Management Language

[Link] ("Matplotlib demo")


[Link] ("x axis caption")
Speed 03
Rich
[Link] ("y axis caption") 07 Library

# [Link](x, y, "bv")# blue and Triangle down marker.


n ters 04
Po i
[Link](x, y,"bo") # blue and circle marker Extensible
Recursion
[Link] ()
05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Formatting the style of the plot:

Example 4 :
import [Link] as plt
01
plt. plot ([1, 2, 3], [4, 5, 1],label="plot one",linewidth=7)
08 Middle Level
plt. plot ([1, 5, 3], [4, 5, 6],label="plot Language
two",linewidth=3) 02
Memory Structured
[Link]('sample line graph') Management Language

[Link]('x values')
Speed 03
[Link]('y values') Rich
[Link](True) 07 Library

[Link]() 04
i n ters
[Link] () Po Extensible
Recursion
[Link] ()
05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
3. Bar Plot (or) Bar chart:

A bar plot is a way of representing data where the length of the bars represents the
01
magnitude/size of the feature/variable.08Bar graphs usually represent numerical and
Middle Level
Language 02
categorical variables grouped in intervals.
Memory Structured
Management Language

The matplotlib API in Python provides the bar() function which can be used in MATLAB
Speed 03
style use or as an object-oriented API. The syntax of the bar() function
Rich to be used with the
07 Library
axes is as follows:-
ers 04
[Link](x, y, width,
P o i n t bottom, Extensible
align)
Recursion

The function creates a bar plot bounded with a05


rectangle depending on the given
parameters.
MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Example 1: to draw the bar plot:

import [Link] as plt


x=[1,2,4,8,9] 01
08 Middle Level
Language 02
y=[1,3,6,6,4] Memory Structured
Management Language

[Link](x,y)
Speed 03
[Link]('sample line graph') Rich
07 Library

[Link]('x values')
n ters 04
i
[Link]('y values') Po Extensible
Recursion
[Link]()
05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Example 2: to draw the bar plot:

import [Link] as plt


01
08 Middle Level
Language 02
[Link]([1, 2, 3], [4, 5, 1],label="plot one")
Memory Structured
Management Language
[Link]([1, 5, 3], [4, 5, 6],label="plot two") #color=g
[Link]('sample line graph') Speed 03
Rich
[Link]('x values') 07 Library

[Link]('y values') 04
i n ters
Po
[Link](True) Extensible
Recursion

[Link]()
05
[Link] ()
MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Example 2: to draw the bar plot:
import numpy as np
import [Link] as plt
# creating the dataset by using dictionary
01
08 Middle Level
data = {'C':20, 'C++':15, 'Java':30,'Python':35} Language 02
Memory Structured
courses = list([Link]()) Management Language

values = list([Link]())
# creating the bar plot Speed 03
Rich
[Link](courses, values, color ='maroon‘, width = 0.4)
07 Library

[Link]("Courses offered")
[Link]("No. of students enrolled") n ters 04
Po i
Extensible
[Link]("Students enrolled in different courses")
Recursion
[Link]()
Output:
05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
4. Histogram:

Here First, we need to understand the differences between the bar graph and
01
08
histogram. A histogram is used for the distribution, whereas
Middle Level a bar chart is used to compare
Language 02
different entities. A histogram is a typeMemory
of bar plot that shows the frequency of a number of
Structured
Management Language
values compared to a set of values ranges.

Speed 03
For example we take the data of the different age group of the people
Rich and plot a histogram
07 Library
with respect to the bin. Now, bin represents the range of values that are divided into series
rs
of intervals known as “bins” . Bins are generally 04
nte created of the same size.
Po i Extensible
Recursion
In Matplotlib, we use the hist() function to create histograms. The hist() function will use
05
an array of numbers to create a histogram.

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Example 1: to draw the Histogram:

from matplotlib import pyplot as plt


population_age = [21,53,49,25,27,30,45] 01
08 Middle Level
bins = [0,10,20,30,40,50,60] Language 02
Memory Structured
[Link](population_age, bins) Management Language

[Link]('age groups')
[Link]('Number of people')
Speed 03
Rich
[Link]('Histogram') 07 Library

[Link]()
n ters 04
Output: Po i
Extensible
Recursion

05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
1. Pie Chart:

The “pie chart” is also known as “circle


01 Pie chart image Example:
chart”, in which a circle is divided into
08 Middle Level
Language 02
sectors that each sector shows percentages
Memory Structured
Management Language
of a whole.

Speed 03
In Matplotlib, we use the pie() function to Rich
07 Library
create piechart

n ters 04
Po i
Extensible
Recursion

05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Example 1: to draw the Pie chart:
from matplotlib import pyplot as plt
values = [3, 12, 5, 8] Example 2: to draw the Pie chart using autopct:

labels = ['a', 'b', 'c', 'd'] 01


08 from matplotlib import pyplot as plt
Middle Level
[Link](values, labels=labels) #autopct?? Language 02
Memory
values = Structured
[3, 12, 5, 8]
Management Language
[Link]() labels = ['a', 'b', 'c', 'd']
Output: # autopct display % of whole.
Speed 03
[Link](values,
Richlabels=labels, autopct='%.2f‘)
07 Library
[Link]()Output:

n ters 04
Po i
Extensible
Recursion

05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Example 3: to draw the Pie chart:
from matplotlib import pyplot as plt
values = [3, 12, 5, 8]
labels = ['a', 'b', 'c', 'd'] 01
08 from matplotlib import pyplot as plt
Middle Level
[Link](values, labels=labels) #autopct?? Language 02
Memory
values = Structured
[3, 12, 5, 8]
Management Language
[Link]() labels = ['a', 'b', 'c', 'd']
Output: # autopct display % of whole.
Speed 03
[Link](values,
Richlabels=labels, autopct='%.2f‘)
07 Library
[Link]()Output:

n ters 04
Po i
Extensible
Recursion

05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Example 3: to draw the Pie chart using explode:

from matplotlib import pyplot as plt


values = [3, 12, 5, 8] 01
08 Middle Level
mylabels = ['a', 'b', 'c', 'd'] Language 02
Memory Structured
[Link](values, labels=mylabels,
Management autopct='%.2f,
Language

explode=[0,0.1,0.1,0,0])
[Link]() Speed 03
Rich
07 Library

n ters 04
Po i
Extensible
Recursion

05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
2. Box plots:

A Box Plot is also known as Whisker plot is created to display the summary of the set of
01
08 firstMiddle
data values having properties like minimum, quartile,
Level median, third quartile and
Language 02
maximum. Memory Structured
Management Language
In the box plot, a box is created from the first quartile to the third quartile, a line is also
there which goes through the box Speed
at the median.
03
Here x-axis denotes the data to be plotted while the y-axis
Richshows the frequency
07 Library
distribution.
rs
In Matplotlib, we use the boxplot() function 04
inte to create box plots.
Po Extensible
Recursion

05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
2. Box plots:

# Import libraries
import [Link] as plt
01
08 Middle Level
Language 02
# create Dataset
Memory Structured
Management Language
y=[2,4,6,8]

Speed 03
# Creating plot Rich

[Link](y) 07 Library

n ters 04
Po i
# show plot Extensible
Recursion
[Link]()
Output:
05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
2. Multiple Box plots:
# Import libraries
import [Link] as plt 01
# create Dataset 08 Middle Level
Language 02
x=[5,10,15,20,25] Memory Structured
Management Language

y=[10,20,30,40,50]
z=[20,30,40,50,60] Speed 03
Rich
data=[x,y,z] 07 Library

# Creating plot
ters 04
[Link](data) Po i n
Extensible
# show plot Recursion

[Link]() 05
Output:
MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
3. Scatter plots:

Scatter plots are used to observe relationship between x-axis and y-axis variables and uses
01
dots to represent the relationship between
08them. Middle
The scatter()
Level method in the matplotlib
Language 02
library is used to draw a scatter plot Memory Structured
Management Language

In Matplotlib, we use the scatter() function to create scatter plots.


Speed 03
Rich
07 Library

n ters 04
Po i
Extensible
Recursion

05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
3. Scatter plots:
import [Link] as plt
# create Dataset 01
08 Middle Level
Language 02
x=[1,5,3,4] Memory Structured
Management Language
y=[2,4,6,8]
# Creating scatter plot Speed 03
Rich
# [Link](x,y,color=‘r’) 07 Library

[Link](x,y) ters 04
i n
Po Extensible
# show plot Recursion

[Link]() 05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Legend alignments:
Syntax:
legend={'loc': 'upper right'}
01
08 Middle Level
upper right Language 02
Memory Structured
upper left Management Language

lower left
lower right
Speed 03
Rich
right 07 Library

center left 04
i n ters
center right Po Extensible
Recursion
lower center
upper center & center 05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
01
08 Middle Level
Language 02
Memory
Management
Structured
Language
Line Plot Attribute:

Speed
Rich
line_type=“o”
03
07 Library

n ters 04
Po i
Extensible
Recursion

05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
Colour plotting:

Characters used for color


01
• 'b‘ blue 08 Middle Level
02
• 'g‘ Green Memory
Management
Language
color=“y”
Structured
Language

• 'r‘ Red
Speed 03
• 'c‘ Cyan Rich
07 Library
• 'm‘ Magenta
• 'y‘ Yellow n ters 04
Po i
Extensible
• 'k‘ Black Recursion

• 'w‘ White 05

MATPLOTLIB UNIT-1 Department of CSE, School of Engineering, Malla Reddy University, Hyderabad.
[Link]

You might also like