Mat Plot Lib
Mat Plot Lib
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
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
[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
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
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.
[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.
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
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
[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:
[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:
[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:
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:
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:
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
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:
• '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]