Python Matplotlib Package
What is Matplotlib?
• Matplotlib is a low level graph plotting library in python
that serves as a visualization utility.
• Matplotlib was created by John D. Hunter.
• Matplotlib is open source and we can use it freely.
• Matplotlib is mostly written in python, a few segments are
written in C, Objective-C and Javascript for Platform
compatibility.
Matplotlib Getting Started
Installation of Matplotlib
• If you have Python and PIP already installed
on a system, then installation of Matplotlib is
very easy.
• Install it using this command:
C:\Users\Your Name>pip install matplotlib
If this command fails, then use a python
distribution that already has Matplotlib
installed, like Anaconda, Spyder etc.
Import Matplotlib
• Once Matplotlib is installed, import it in your
applications by adding
the import module statement:
import matplotlib
Matplotlib Pyplot
Pyplot
• Most of the Matplotlib utilities
lies under
the pyplot submodule, and are
usually imported under
the plt alias:
import matplotlib.pyplot as plt
Eg: Draw a line in a diagram from
position (0,0) to position (6,250):
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([0, 6])
ypoints = np.array([0, 250])
plt.plot(xpoints, ypoints)
plt.show()
o/p:
Matplotlib Plotting
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.
• Parameter 2 is an array containing the points on the y-
axis.
• If we need to plot a line from (1, 3) to (8, 10), we have
to pass two arrays [1, 8] and [3, 10] to the plot
function.
Eg:Draw a line in a diagram
from position (1, 3) to
position (8, 10):
import matplotlib.pyplot
as plt
import numpy as np
xpoints = np.array([1, 8])
ypoints = np.array([3, 10])
plt.plot(xpoints, ypoints)
plt.show()
Matplotlib Markers
Markers
• You can use the keyword
argument marker to
emphasize each point with a
specified marker:
Eg: Mark each point with a circle:
import matplotlib.pyplot as plt
import numpy as np
ypoints =
np.array([3, 8, 1, 10])
plt.plot(ypoints, marker = 'o')
plt.show()
Matplotlib Line
Linestyle
• You can use the keyword
argument linestyle, or
shorter ls, to change the
style of the plotted line:
Example
• Use a dotted line:
import matplotlib.pyplot as
plt
import numpy as np
ypoints =
np.array([3, 8, 1, 10])
plt.plot(ypoints, linestyle
= 'dotted')
plt.show()
Use a dashed line:
#Three lines to make our compiler
able to draw:
import sys
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, linestyle = 'dashed')
plt.show()
#Two lines to make our compiler able to
draw:
plt.savefig(sys.stdout.buffer)
sys.stdout.flush()
Matplotlib Labels
Create Labels for a Plot
• With Pyplot, you can use
the xlabel() and ylabel() functio
ns to set a label for the x- and y-
axis.
Eg:Add labels to the x- and y-axis:
import numpy as np
import matplotlib.pyplot as plt
x=
np.array([80, 85, 90, 95, 100, 10
5, 110, 115, 120, 125])
y=
np.array([240, 250, 260, 270, 28
0, 290, 300, 310, 320, 330])
plt.plot(x, y)
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.show()
Matplotlib Adding Grid Lines
Add Grid Lines to a Plot
• With Pyplot, you can use
the grid() function to add grid lines
to the plot.
Eg:Add grid lines to the plot:
import numpy as np
import matplotlib.pyplot as plt
x=
np.array([80, 85, 90, 95, 100, 105,
110, 115, 120, 125])
y=
np.array([240, 250, 260, 270, 280,
290, 300, 310, 320, 330])
plt.title("Sports Watch Data")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x, y)
plt.grid()
plt.show()
Matplotlib Subplot
Display Multiple Plots
• With the subplot() function you can draw multiple plots in one figure:
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()
Matplotlib Scatter
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:
Eg:
import matplotlib.pyplot as plt
import numpy as np
x=
np.array([5,7,8,7,2,17,2,9,4,11,12,9
,6])
y=
np.array([99,86,87,88,111,86,103,8
7,94,78,77,85,86])
plt.scatter(x, y)
plt.show()
Matplotlib Bars
Creating Bars
• With Pyplot, you can use
the bar() function to draw
bar graphs:
Eg:Draw 4 bars:
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()
Matplotlib Histograms
• A histogram is a graph showing frequency distributions.
• It is a graph showing the number of observations within each given interval.
• Example: Say you ask for the height of 250 people, you might end up with a
histogram like this:
#Three lines to make our compiler able to draw:
import sys
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
x = np.random.normal(170, 10, 250)
plt.hist(x)
plt.show()
#Two lines to make our compiler able to draw:
plt.savefig(sys.stdout.buffer)
sys.stdout.flush()
Matplotlib Pie Charts
Creating Pie Charts
• With Pyplot, you can use
the pie() function to draw pie charts:
#Three lines to make our compiler able to
draw:
import sys
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
plt.pie(y)
plt.show()
#Two lines to make our compiler able to
draw:
plt.savefig(sys.stdout.buffer)
sys.stdout.flush()