DEV Community

Onaolapo-11
Onaolapo-11

Posted on

I looked at the subplot() function in Matplotlib

Day 61 [October 7, 2025]

I need to buckle down, as I'm still lagging on day 2's remaining goals, day 3 & 4 goals, "Day 3-4: Control structures (if-else, loops)", as well as day 5 (and 6) goals, "Day 5-6: Functions and modules", and Day 7 target (exercises) (Meta AI, personal communication, August 8, 2025). If I haven't covered this, I can't make progress on day 8 - 60 goals.

Goals:
As extracted from the 'Python for Software Development' textbook by Halvorsen (n.d.):

  • The New Age of Programming ✅
  • What is Python? ✅
  • Introduction to Python ✅
  • Interpreted vs. Compiled ✅
  • Python Packages ✅
  • Python Packages for Science and Numerical Computations ✅
  • Python Editors ✅
  • Python IDLE ✅
  • Visual Studio Code ✅
  • Variables ✅
  • Numbers ✅
  • Strings ✅
  • String Input✅
  • Built-in Functions✅
  • Python Standard Library✅
  • Using Python Libraries, Packages and Modules✅
  • Python Packages✅
  • Plotting in Python ✅
  • Subplots✅
  • Exercises
  • If ... Else
  • Arrays
  • For Loops
  • Nested
  • For Loops
  • While Loops
  • Exercises
  • Creating Functions in Python - Introduction
  • Functions with multiple return values
  • Exercises
  • Creating Classes in Python
  • The init () Function
  • Exercises
  • Creating Python Modules
  • Exercises

Notes:
'Python for Software Development' textbook by Halvorsen (n.d.):
To show more than one plot on the same window (in one figure) in matplotlib, we use the subplot() function (Halvorsen, n.d.; w3schools, n.d.).

How is it partitioned?
By typing "subplot(m,n,p)" (Halvorsen, n.d.), essentially:

  • the figure window is divided into an m x n matrix (m: row, n: column), with p being the subplot for the current subplot as the code copied from w3schools (n.d.) indicates:

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()

Switching the values of p in the code above switches the subplots.

Many subplots can be shown on one window as much we want, but the key factor is (m,n,p) must be adequately specified (w3schools, n.d.).

To indicate a title to any subplot, we utilize the title() function and to title the entire window, we use the suptitle() function (w3schools, n.d.), i.e. plt.title('TITLE') and plt.suptitle('SUPER TITLE') respectively. Adjusting the above code, we have:

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)
plt.title('ARSENAL FORM')

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.title("VICTOR SALARY")

plt.suptitle('UPDATE')

plt.show()

Summary:
I looked at the subplot() function in Matplotlib.

References:

  1. Halvorsen, H. (n.d.). Python. https://halvorsen.blog/documents/programming/python/python.php#python4

  2. w3schools. (n.d.). Display multiple plots. https://www.w3schools.com/python/matplotlib_subplot.asp

Top comments (0)