0
0
Matplotlibdata~10 mins

Multiple images in subplot grid in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple images in subplot grid
Start
Create Figure and Subplots Grid
Load or Generate Images
Assign Each Image to a Subplot
Display Images with plt.imshow()
Adjust Layout and Show Plot
End
The flow shows creating a grid of subplots, loading images, placing each image in a subplot, and displaying them together.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

fig, axs = plt.subplots(2, 2)
for i in range(2):
    for j in range(2):
        axs[i, j].imshow(np.random.rand(10,10))
plt.show()
This code creates a 2x2 grid of subplots and displays a random image in each subplot.
Execution Table
StepActionVariable/FunctionResult/State
1Create figure and 2x2 subplots gridplt.subplots(2, 2)fig object and axs 2x2 array created
2Start outer loop i=0i=0Ready to fill first row
3Start inner loop j=0j=0Ready to fill subplot axs[0,0]
4Generate random imagenp.random.rand(10,10)10x10 array with random floats
5Display image in axs[0,0]axs[0,0].imshow()Image shown in first subplot
6Inner loop j=1j=1Ready to fill subplot axs[0,1]
7Generate random imagenp.random.rand(10,10)10x10 array with random floats
8Display image in axs[0,1]axs[0,1].imshow()Image shown in second subplot
9Outer loop i=1i=1Ready to fill second row
10Inner loop j=0j=0Ready to fill subplot axs[1,0]
11Generate random imagenp.random.rand(10,10)10x10 array with random floats
12Display image in axs[1,0]axs[1,0].imshow()Image shown in third subplot
13Inner loop j=1j=1Ready to fill subplot axs[1,1]
14Generate random imagenp.random.rand(10,10)10x10 array with random floats
15Display image in axs[1,1]axs[1,1].imshow()Image shown in fourth subplot
16Show all subplotsplt.show()Window opens with 4 images in grid
17End-All images displayed in subplot grid
💡 All subplots filled with images and displayed, execution ends.
Variable Tracker
VariableStartAfter Step 4After Step 7After Step 11After Step 14Final
iN/A00111
jN/A01011
axsN/A2x2 Axes array2x2 Axes array2x2 Axes array2x2 Axes array2x2 Axes array
image_arrayN/Arandom 10x10 arrayrandom 10x10 arrayrandom 10x10 arrayrandom 10x10 arraylast random 10x10 array
Key Moments - 3 Insights
Why do we use axs[i, j].imshow() inside nested loops?
Because axs is a 2D array of subplot axes, using axs[i, j] targets the correct subplot to display each image, as shown in steps 5, 8, 12, and 15.
What happens if we call plt.imshow() without specifying the subplot?
It will display the image on the current active axes or create a new figure, not placing images in the grid properly. The execution_table shows images assigned explicitly to axs[i, j].
Why do we call plt.show() only once after the loops?
plt.show() renders the entire figure with all subplots at once. Calling it inside loops would open multiple windows. Step 16 shows the single call after all images are set.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'i' when the image is displayed in axs[1,0]?
A2
B0
C1
DNone
💡 Hint
Check step 12 in the execution_table where axs[1,0] is used.
At which step does the inner loop variable 'j' change from 0 to 1 for the first time?
AStep 6
BStep 3
CStep 5
DStep 7
💡 Hint
Look at the execution_table rows where j changes inside the inner loop.
If we change plt.subplots(2, 2) to plt.subplots(3, 1), how would the variable 'axs' change?
AIt becomes a 3x1 array of axes
BIt becomes a 1D array with 3 axes
CIt becomes a single Axes object
DIt becomes a 2x3 array of axes
💡 Hint
Check how plt.subplots returns axes arrays depending on rows and columns.
Concept Snapshot
Use plt.subplots(rows, cols) to create a grid of subplots.
Each subplot is accessed by axs[row, col] if rows and cols > 1.
Use axs[i, j].imshow(image) to display images in each subplot.
Call plt.show() once after all images are assigned.
This displays multiple images neatly in a grid layout.
Full Transcript
This visual execution trace shows how to display multiple images in a grid using matplotlib. First, a figure and a 2x2 grid of subplots are created. Then, nested loops iterate over rows and columns. At each subplot position axs[i, j], a random 10x10 image array is generated and displayed using imshow. After filling all subplots, plt.show() displays the window with all images arranged in the grid. Variables i and j track the current subplot indices. The variable axs holds the array of subplot axes. Key moments clarify why images are assigned to specific subplots and why plt.show() is called once at the end. The quiz tests understanding of loop variables and subplot structure. This method helps visualize multiple images side by side clearly.