Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 09

Several Plots in One Figure

Split one figure into a grid with plt.subplot, give each panel its own title, and add one heading over the whole figure with suptitle.

Intermediate25 min
Several Plots in One Figure hero Module 25 Lesson 9Several Plots in One Figure hero Module 25 Lesson 9

Piling six lines onto one set of axes makes a thicket. Splitting the figure into panels keeps each measurement readable while the eye still compares them side by side. plt.subplot(rows, cols, index) describes the grid and says which cell the next chart goes into.

A two by three grid of panels numbered one to sixA two by three grid of panels numbered one to six

Two panels side by side

plt.subplot(1, 2, 1) means: one row, two columns, and the next chart belongs in cell one. Everything you draw afterwards lands in that cell until the next subplot call moves you on. The index starts at 1, not 0.

python
import matplotlib.pyplot as plt

days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
signups = [42, 58, 51, 66, 73]
revenue = [1840, 2510, 2260, 2980, 3310]

plt.subplot(1, 2, 1)
plt.plot(days, signups, marker="o")
plt.title("Signups")

plt.subplot(1, 2, 2)
plt.plot(days, revenue, marker="s", color="#EC4899")
plt.title("Revenue")

plt.tight_layout()
plt.show()

print("two panels, two scales, no squashing")

Output:

text
two panels, two scales, no squashing

Stacked instead of side by side

Swap the first two numbers and the panels stack. Two rows and one column put the charts one above the other, which suits a shared x axis: the same days line up vertically, so the reader can compare a single day across both.

python
import matplotlib.pyplot as plt

days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
signups = [42, 58, 51, 66, 73]
revenue = [1840, 2510, 2260, 2980, 3310]

plt.subplot(2, 1, 1)
plt.plot(days, signups, marker="o")
plt.title("Signups")

plt.subplot(2, 1, 2)
plt.plot(days, revenue, marker="s", color="#EC4899")
plt.title("Revenue")

plt.tight_layout()
plt.show()

print("same days, stacked, easy to read down a column")

Output:

text
same days, stacked, easy to read down a column
Two stacked panels, the first above the secondTwo stacked panels, the first above the second

A grid of panels

Any grid works. Six charts fit into two rows of three, and the index runs along the first row before dropping to the second. A loop keeps the code short when the charts are variations on one idea.

python
import matplotlib.pyplot as plt

depots = ["Northgate", "Eastport", "Westfield", "Southbank"]
weekly = {
    "Northgate": [12, 18, 14, 21, 25],
    "Eastport": [9, 11, 15, 13, 12],
    "Westfield": [22, 19, 24, 28, 31],
    "Southbank": [5, 8, 7, 11, 14],
}
days = ["Mon", "Tue", "Wed", "Thu", "Fri"]

plt.figure(figsize=(10, 6))
for index, depot in enumerate(depots, start=1):
    plt.subplot(2, 2, index)
    plt.plot(days, weekly[depot], marker="o")
    plt.title(depot)

plt.tight_layout()
plt.show()

for depot in depots:
    print(f"{depot:<10} {sum(weekly[depot])}")

Output:

text
Northgate  90
Eastport   60
Westfield  124
Southbank  45

One heading over the whole figure

plt.title names one panel. plt.suptitle names the figure, so it sits above all of them. Use both: the super title for the subject, each panel title for the detail.

python
import matplotlib.pyplot as plt

days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
signups = [42, 58, 51, 66, 73]
revenue = [1840, 2510, 2260, 2980, 3310]

plt.figure(figsize=(9, 4))

plt.subplot(1, 2, 1)
plt.plot(days, signups, marker="o")
plt.title("Signups")

plt.subplot(1, 2, 2)
plt.plot(days, revenue, marker="s", color="#EC4899")
plt.title("Revenue")

plt.suptitle("Last week at a glance", fontsize=15)
plt.tight_layout()
plt.show()

print("suptitle sits above every panel")

Output:

text
suptitle sits above every panel

Key takeaways

  • plt.subplot(rows, cols, index) picks the cell for the next chart

  • The index starts at 1 and runs left to right, then down

  • Swapping rows and columns turns side by side into stacked

  • plt.title names a panel; plt.suptitle names the figure

  • Next: Working with Figure and Axes Objects

  • Previous: Axis Limits and Ticks

Loading playground…

Check your understanding

Several Plots in One Figure Quiz

What do the three numbers in subplot(2, 3, 4) mean?

1 / 10