
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Python libraries includes pandas, matplotlib, seaborn.
Typology: Slides
1 / 32
This page cannot be seen from the preview
Don't miss anything!

























Department of CSE, School of Engineering, Malla Reddy
Middle Level
Language
Structured
Language
Rich
Library
Extensible
Recursion
Pointers
Speed
Memory
Management
Department of CSE, School of Engineering, Malla Reddy
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 MATLAB‟s.
The multidimensional MATPLOTLIB built on NumPy arrays
Installation:
Matplotlib and its dependency packages are installed , when we install
the Matplotlib by using following pip command.
matplotlib.pyplot is a collection of command style functions.
Each pyplot function makes some change to a figure. For example, a
function creates a figure, a plotting area in a figure, plots some lines
in a plotting area, decorates the plot with labels, etc.
Middle Level
Language
Structured
Language
Rich
Library
Extensible
Recursion
Pointers
Speed
Memory
Management
Another one of the most important sections on this page is
gallery section
It shows all the kind of plots/figures that Matplotlib is capable of
creating for you. You can select anyone of those, and it takes you the
example page having the figure and very well documented code.
Another important page is
It shows the documentation functions in it.
Department of CSE, School of Engineering, Malla Reddy
Middle Level
Language
Structured
Language
Rich
Library
Extensible
Recursion
Pointers
Speed
Memory
Management
1: Basic Plots using Matplotlib
1. Area Plots or area chart
3: Advanced Visualization Tools using Matplotlib
Department of CSE, School of Engineering, Malla Reddy
Middle Level
Language
Structured
Language
Rich
Library
Extensible
Recursion
Pointers
Speed
Memory
Management
Department of CSE, School of Engineering, Malla Reddy
import matplotlib.pyplot as
plt
# Create data
x=[1,2,3,4,6]
y=[1,4,5,8,4]
# Area plot
plt.fill_between(x, y)
plt.show()
Middle Level
Language
Structured
Language
Rich
Library
Extensible
Recursion
Pointers
Speed
Memory
Management
Department of CSE, School of Engineering, Malla Reddy
import matplotlib.pyplot as
plt
# Create data
# x=range(1,6)
x=[1,2,3,4,5]
y=[1,4,5,8,5]
# Area plot
plt.stackplot(x,y)
plt.show()
Output:
Middle Level
Language
Structured
Language
Rich
Library
Extensible
Recursion
Pointers
Speed
Memory
Management
Department of CSE, School of Engineering, Malla Reddy
import matplotlib.pyplot
as plt
plt. plot ([1, 2, 3], [4, 5,
1])
plt.show ()
Middle Level
Language
Structured
Language
Rich
Library
Extensible
Recursion
Pointers
Speed
Memory
Management
Department of CSE, School of Engineering, Malla Reddy
x=[1,2,4,8,9]
y=[1,3,6,6,4]
plt.plot(x,y)
plt.title('sample line graph')
plt.xlabel('x values')
plt.ylabel('y values')
plt.show()
Middle Level
Language
Structured
Language
Rich
Library
Extensible
Recursion
Pointers
Speed
Memory
Management
Example 4 :
import matplotlib.pyplot as plt
plt. plot ([1, 2, 3], [4, 5, 1],label="plot one",linewidth=7)
plt. plot ([1, 5, 3], [4, 5, 6],label="plot two",linewidth=3)
plt.title('sample line graph')
plt.xlabel('x values')
plt.ylabel('y values')
plt.grid(True)
plt.legend()
plt.show ()
plt.show ()
Department of CSE, School of Engineering, Malla Reddy
Middle Level
Language
Structured
Language
Rich
Library
Extensible
Recursion
Pointers
Speed
Memory
Management
Department of CSE, School of Engineering, Malla Reddy
A bar plot is a way of representing data where the length of the bars
represents the magnitude/size of the feature/variable. Bar graphs
usually represent numerical and categorical variables grouped in intervals.
The matplotlib API in Python provides the bar() function which can be
used in MATLAB style use or as an object-oriented API. The syntax of the
bar() function to be used with the axes is as follows:-
plt.bar(x, y, width, bottom, align)
The function creates a bar plot bounded with a rectangle depending on
the given parameters.
Middle Level
Language
Structured
Language
Rich
Library
Extensible
Recursion
Pointers
Speed
Memory
Management
Department of CSE, School of Engineering, Malla Reddy
Example 2: to draw the bar plot:
Middle Level
Language
Structured
Language
Rich
Library
Extensible
Recursion
Pointers
Speed
Memory
Management
Department of CSE, School of Engineering, Malla Reddy
i mport numpy as np
import matplotlib.pyplot as plt
data = {'C':20, 'C++':15,
'Java':30,'Python':35}
courses = list(data.keys())
values = list(data.values())
plt.bar(courses, values, color ='maroon‘,
width = 0.4)
plt.xlabel("Courses offered")
plt.ylabel("No. of students enrolled")
plt.title("Students enrolled in different
courses")
Example 2: to draw the bar plot:
Middle Level
Language
Structured
Language
Rich
Library
Extensible
Recursion
Pointers
Speed
Memory
Management
Department of CSE, School of Engineering, Malla Reddy
from matplotlib import pyplot as plt
population_age =
bins = [0,10,20,30,40,50,60]
plt.hist(population_age, bins)
plt.xlabel('age groups')
plt.ylabel('Number of people')
plt.title('Histogram')
plt.show()
Output:
Example 1: to draw the Histogram:
Middle Level
Language
Structured
Language
Rich
Library
Extensible
Recursion
Pointers
Speed
Memory
Management
Department of CSE, School of Engineering, Malla Reddy
The “ pie chart” is also known as
“circle chart”, in which a circle is
divided into sectors that each
sector shows percentages of a
whole.
In Matplotlib, we use the pie()
function to create piechart