The Scientific Python ecosystem:
Matplotlib
Online Workshop on Basics of Python for Engineering Education
Dr. Bikramjit Goswami
Department of Electrical and Electronics Engineering
Assam Don Bosco University
Matplotlib
Matplotlib is a graph plotting library in python that serves as a
visualization utility.
Pyplot
Most of the Matplotlib utilities lies under the pyplot submodule, and
are usually imported under the plt alias:
import [Link] as plt
Now the Pyplot package can be referred to as plt.
08-08-2025 3
Example
Draw a line in a diagram from position (0 , 0) to position (10 , 200)
The program
import matplotlib
import [Link] as plt
import numpy as np
xpoints = [Link]([0, 10])
08-08-2025 5
The program
ypoints = [Link]([0, 200])
[Link](xpoints, ypoints)
08-08-2025 6
The plot
08-08-2025 7
Try another plotting
import numpy as np
import [Link] as plt
x = [Link](0,10)
y = [Link](-10,0)
[Link](x,y)
[Link]('x-axis')
[Link]('y-axis')
[Link]('My linear plot')
08-08-2025 8
08-08-2025 9
Try one more plot
import numpy as np
import [Link] as plt
a = [0, -100, 25, 67, -323]
b = [0, 3, 7, 3, 9]
[Link](a,b)
08-08-2025 10
08-08-2025 11
Selecting a part of the figure
import numpy as np
import [Link] as plt
a = [0, -100, 25, 67, -323]
b = [0, 3, 7, 3, 9]
[Link]([-50, 75, 2, 8])
[Link](a,b)
08-08-2025 12
08-08-2025 13
Try One more plotting
import [Link] as plt
[Link]([1,2,3,4],[1,4,9,16])
[Link]()
08-08-2025 14
08-08-2025 15
Labels and the Title
import [Link] as plt
x=[1, 2, 3]
y=[5, 7, 4]
[Link](x,y)
[Link]('x-axis')
[Link]('y-axis')
[Link]('My first plot')
08-08-2025 16
08-08-2025 17
Plotting a sine wave
import numpy as np
import [Link] as plt
t=[Link](0, 10, 0.1)
y=[Link](t)
[Link](t,y)
08-08-2025 18
08-08-2025 19
Additional features in the plot
[Link](t,y)
[Link]('Sine Wave’)
[Link]('Time’)
[Link]('sin(t)’)
[Link]()
08-08-2025 20
08-08-2025 21
Draw 2 plots side-by-side
import [Link] as plt
import numpy as np
x=[Link]([0, 1, 2, 3])
y=[Link]([3, 8, 1, 10])
z=[Link]([10, 20, 30, 40])
[Link](1, 2, 1)
[Link](x , y)
[Link](1, 2, 2)
[Link](x , z)
08-08-2025 22
08-08-2025 23
Draw two plots one above the other
import numpy as np
t = [Link](0 , 10 , 0.5)
[Link](2 , 1 , 1)
[Link](t , [Link](t))
[Link](2 , 1 , 2)
[Link](t , [Link](t))
08-08-2025 24
08-08-2025 25
Drawing multiple plots on the same figure
[Link](2,2,1)
[Link](t, [Link](t))
[Link](2,2,2)
[Link](t, [Link](t))
[Link](2,2,3)
[Link](t, [Link](t))
[Link](2,2,4)
[Link](t, [Link](t))
08-08-2025 26
08-08-2025 27
Multiple plots in the same figure
import [Link] as plt
x1 = [1, 2, 3]
y1 = [5, 7, 4]
x2 = [1, 2, 3]
y2 = [10, 14, 12]
[Link](x1, y1, label='First Line')
[Link](x2, y2, label='Second Line')
[Link]('x-axis')
[Link]('y-axis')
[Link]('Two lines')
[Link]()
[Link]()
08-08-2025 28
08-08-2025 29
Plotting a Bar diagram
import numpy as np
import [Link] as plt
x=[Link]([0, 1, 2, 3])
y=[Link]([4, 7, 3, 1])
[Link](x,y)
08-08-2025 30
08-08-2025 31
Plotting Horizontal Bar Diagram
x=[Link](["A", "B", "C", "D"])
y=[Link]([3, 8, 1, 6])
[Link](x,y)
08-08-2025 32
08-08-2025 33
import [Link] as plt
x1 = [1, 3, 5, 7, 9]
y1 = [6, 3, 8, 2, 4]
x2 = [2, 4, 6, 8, 10]
y2 = [7, 8, 3, 4, 2]
Multiple Bars in
the same figure [Link](x1, y1, label='Bar1', color= 'red')
[Link](x2, y2, label='Bar2', color= 'blue')
[Link]('x -->')
[Link]('y -->')
[Link]('Comparison of even and odd bars')
[Link]()
[Link]()
08-08-2025 34
08-08-2025 35
Plotting a Histogram
• A histogram is a graph showing frequency distributions.
• Let us plot the range of weights of some kids in a class
of 20 students:
• 5 students having a weight of 25 kg
• 3 students having a weight of 27 kg
• 2 students having a weight of 29 kg
• 4 students having a weight of 30 kg
• 6 students having a weight of 32 kg
08-08-2025 36
The frequency of weight distribution is
obtained by the following program
x=[Link]([25, 25, 25, 25, 25, 27,
27, 27, 29, 29, 30, 30, 30, 30, 32,
32, 32, 32, 32, 32])
[Link](x)
08-08-2025 37
Plotting a Pi Chart
import numpy as np
import [Link] as plt
y=[Link]([35, 25, 25, 15])
mylabels=["Apples", "Bananas", "Cherries", "Dates"]
[Link](y, labels=mylabels)
08-08-2025 38
08-08-2025 39
Indicating the Percentage of the portion
import numpy as np
import [Link] as plt
y=[Link]([35, 25, 25, 15])
mylabels=["Apples", "Bananas", "Cherries", "Dates"]
[Link](y, labels=mylabels, autopct='%1.1f')
08-08-2025 40
08-08-2025 41
Scatter Plot
import [Link] as plt
x = [1,2,3,4,3.3,6,7,3.6,9]
y = [5,2,4,4.1,4.2,4.5,2.8,3.7,3]
[Link](x,y)
[Link]('x')
[Link]('y')
[Link]('My first Scatter Plot')
08-08-2025 42
08-08-2025 43
import [Link] as plt
x = [1,2,3,4,3.3,6,7,3.6,9]
y = [5,2,4,4.1,4.2,4.5,2.8,3.7,3]
Changing
[Link](x,y, marker='*') the default
marker
[Link]('x')
[Link]('y')
[Link]('My first Scatter Plot')
08-08-2025 44
08-08-2025 45
Stack Plot
import [Link] as plt
days = [1,2,3,4,5]
sleeping = [7,8,6,11,7]
eating = [2,3,4,3,2]
working = [7,8,7,2,2]
playing = [8,5,7,8,13]
[Link](days, sleeping, eating, working, playing)
[Link](‘Days')
[Link](‘Hours')
[Link](‘Stackplot')
[Link]()
08-08-2025 46
08-08-2025 47