0
0
Matplotlibdata~30 mins

Multiple images in subplot grid in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Multiple images in subplot grid
📖 Scenario: You are working as a data scientist who needs to display multiple images side by side for easy comparison. This is common when analyzing photos, medical scans, or satellite images.
🎯 Goal: Create a grid of subplots using matplotlib and display four different images in this grid.
📋 What You'll Learn
Create a list called images containing four 2D numpy arrays representing grayscale images.
Create a variable called fig, axes using plt.subplots(2, 2) to make a 2x2 grid of subplots.
Use a for loop with variables ax and img to iterate over axes.flat and images simultaneously.
Inside the loop, display each image on its subplot using ax.imshow(img, cmap='gray').
Turn off axis ticks and labels for each subplot using ax.axis('off').
Finally, use plt.show() to display the figure.
💡 Why This Matters
🌍 Real World
Displaying multiple images side by side helps compare different photos or scans quickly, useful in medical imaging, satellite photo analysis, or quality control.
💼 Career
Data scientists and analysts often need to visualize multiple images together to spot patterns, differences, or anomalies.
Progress0 / 4 steps
1
Create four sample images
Create a list called images containing four 5x5 numpy arrays with these exact values: first is all zeros, second is all ones, third is a 5x5 identity matrix, and fourth is a 5x5 matrix of twos.
Matplotlib
Need a hint?

Use np.zeros, np.ones, np.eye, and np.full to create the arrays.

2
Create a 2x2 subplot grid
Import matplotlib.pyplot as plt. Then create a 2x2 grid of subplots using fig, axes = plt.subplots(2, 2).
Matplotlib
Need a hint?

Use plt.subplots(2, 2) to create the grid.

3
Display images in the subplot grid
Use a for loop with variables ax and img to iterate over axes.flat and images simultaneously. Inside the loop, display each image on its subplot using ax.imshow(img, cmap='gray') and turn off axis ticks and labels with ax.axis('off').
Matplotlib
Need a hint?

Use zip(axes.flat, images) to loop over both lists together.

4
Show the figure with images
Add a line to display the figure using plt.show().
Matplotlib
Need a hint?

Use plt.show() to display the plot window.