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 matplotlib.pyplot 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 matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([0, 10])
08-08-2025 5
The program
ypoints = np.array([0, 200])
plt.plot(xpoints, ypoints)
08-08-2025 6
The plot
08-08-2025 7
Try another plotting
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0,10)
y = np.arange(-10,0)
plt.plot(x,y)
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('My linear plot')
08-08-2025 8
08-08-2025 9
Try one more plot
import numpy as np
import matplotlib.pyplot as plt
a = [0, -100, 25, 67, -323]
b = [0, 3, 7, 3, 9]
plt.plot(a,b)
08-08-2025 10
08-08-2025 11
Selecting a part of the figure
import numpy as np
import matplotlib.pyplot as plt
a = [0, -100, 25, 67, -323]
b = [0, 3, 7, 3, 9]
plt.axis([-50, 75, 2, 8])
plt.plot(a,b)
08-08-2025 12
08-08-2025 13
Try One more plotting
import matplotlib.pyplot as plt
plt.plot([1,2,3,4],[1,4,9,16])
plt.show()
08-08-2025 14
08-08-2025 15
Labels and the Title
import matplotlib.pyplot as plt
x=[1, 2, 3]
y=[5, 7, 4]
plt.plot(x,y)
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('My first plot')
08-08-2025 16
08-08-2025 17
Plotting a sine wave
import numpy as np
import matplotlib.pyplot as plt
t=np.arange(0, 10, 0.1)
y=np.sin(t)
plt.plot(t,y)
08-08-2025 18
08-08-2025 19
Additional features in the plot
plt.plot(t,y)
plt.title('Sine Wave’)
plt.xlabel('Time’)
plt.ylabel('sin(t)’)
plt.grid()
08-08-2025 20
08-08-2025 21
Draw 2 plots side-by-side
import matplotlib.pyplot as plt
import numpy as np
x=np.array([0, 1, 2, 3])
y=np.array([3, 8, 1, 10])
z=np.array([10, 20, 30, 40])
plt.subplot(1, 2, 1)
plt.plot(x , y)
plt.subplot(1, 2, 2)
plt.plot(x , z)
08-08-2025 22
08-08-2025 23
Draw two plots one above the other
import numpy as np
t = np.arange(0 , 10 , 0.5)
plt.subplot(2 , 1 , 1)
plt.plot(t , np.sin(t))
plt.subplot(2 , 1 , 2)
plt.plot(t , np.cos(t))
08-08-2025 24
08-08-2025 25
Drawing multiple plots on the same figure
plt.subplot(2,2,1)
plt.plot(t, np.sin(t))
plt.subplot(2,2,2)
plt.plot(t, np.cos(t))
plt.subplot(2,2,3)
plt.plot(t, np.exp(t))
plt.subplot(2,2,4)
plt.plot(t, np.log(t))
08-08-2025 26
08-08-2025 27
Multiple plots in the same figure
import matplotlib.pyplot as plt
x1 = [1, 2, 3]
y1 = [5, 7, 4]
x2 = [1, 2, 3]
y2 = [10, 14, 12]
plt.plot(x1, y1, label='First Line')
plt.plot(x2, y2, label='Second Line')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Two lines')
plt.legend()
plt.show()
08-08-2025 28
08-08-2025 29
Plotting a Bar diagram
import numpy as np
import matplotlib.pyplot as plt
x=np.array([0, 1, 2, 3])
y=np.array([4, 7, 3, 1])
plt.bar(x,y)
08-08-2025 30
08-08-2025 31
Plotting Horizontal Bar Diagram
x=np.array(["A", "B", "C", "D"])
y=np.array([3, 8, 1, 6])
plt.barh(x,y)
08-08-2025 32
08-08-2025 33
import matplotlib.pyplot 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 plt.bar(x1, y1, label='Bar1', color= 'red')
plt.bar(x2, y2, label='Bar2', color= 'blue')
plt.xlabel('x -->')
plt.ylabel('y -->')
plt.title('Comparison of even and odd bars')
plt.legend()
plt.show()
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=np.array([25, 25, 25, 25, 25, 27,
27, 27, 29, 29, 30, 30, 30, 30, 32,
32, 32, 32, 32, 32])
plt.hist(x)
08-08-2025 37
Plotting a Pi Chart
import numpy as np
import matplotlib.pyplot as plt
y=np.array([35, 25, 25, 15])
mylabels=["Apples", "Bananas", "Cherries", "Dates"]
plt.pie(y, labels=mylabels)
08-08-2025 38
08-08-2025 39
Indicating the Percentage of the portion
import numpy as np
import matplotlib.pyplot as plt
y=np.array([35, 25, 25, 15])
mylabels=["Apples", "Bananas", "Cherries", "Dates"]
plt.pie(y, labels=mylabels, autopct='%1.1f')
08-08-2025 40
08-08-2025 41
Scatter Plot
import matplotlib.pyplot 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]
plt.scatter(x,y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('My first Scatter Plot')
08-08-2025 42
08-08-2025 43
import matplotlib.pyplot 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
plt.scatter(x,y, marker='*') the default
marker
plt.xlabel('x')
plt.ylabel('y')
plt.title('My first Scatter Plot')
08-08-2025 44
08-08-2025 45
Stack Plot
import matplotlib.pyplot 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]
plt.stackplot(days, sleeping, eating, working, playing)
plt.xlabel(‘Days')
plt.ylabel(‘Hours')
plt.title(‘Stackplot')
plt.show()
08-08-2025 46
08-08-2025 47