We use imshow to show pictures or image data on the screen. It helps us see what the image looks like in a simple way.
Displaying images with imshow in Matplotlib
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Matplotlib
matplotlib.pyplot.imshow(X, cmap=None, interpolation=None) # X is the image data (array or matrix) # cmap sets the color map (for grayscale or color) # interpolation smooths the image display
X is usually a 2D or 3D array representing the image pixels.
cmap is useful for grayscale images to choose how shades appear.
Examples
Matplotlib
import matplotlib.pyplot as plt import numpy as np image = np.random.rand(10, 10) plt.imshow(image) plt.show()
Matplotlib
plt.imshow(image, cmap='gray')
plt.show()Matplotlib
color_image = np.random.rand(10, 10, 3) plt.imshow(color_image) plt.show()
Sample Program
This program creates a small 5x5 image where pixel values increase from top-left to bottom-right. It shows the image with a color map and a color bar to explain the colors.
Matplotlib
import matplotlib.pyplot as plt import numpy as np # Create a simple 5x5 image with a gradient image = np.array([[i + j for j in range(5)] for i in range(5)]) plt.imshow(image, cmap='viridis') plt.colorbar() # Show color scale plt.title('Simple Gradient Image') plt.show()
Important Notes
Always call plt.show() to display the image window.
You can add plt.colorbar() to show the color scale next to the image.
Images can be grayscale (2D arrays) or color (3D arrays with 3 channels for RGB).
Summary
imshow shows image data as a picture.
You can control colors with cmap and smoothness with interpolation.
Use plt.show() to see the image on screen.
Practice
1. What does the
imshow function in matplotlib do?easy
Solution
Step 1: Understand the purpose of
imshowimshowis designed to display image data visually as a picture.Step 2: Compare with other plotting functions
Other functions like line plots or histograms serve different purposes, so they don't matchimshow's role.Final Answer:
Displays image data as a picture -> Option AQuick Check:
imshow= display image [OK]
Hint: Remember: imshow means 'image show' [OK]
Common Mistakes:
- Confusing imshow with plot or hist functions
- Thinking imshow saves images instead of displaying
- Assuming imshow creates charts, not images
2. Which of the following is the correct way to display a 2D numpy array named
img as an image using matplotlib?easy
Solution
Step 1: Identify the function to display images
To show an image from a 2D array,plt.imshow()is the correct function.Step 2: Check other options for correctness
plt.plot()is for line plots,plt.hist()for histograms, andplt.show()displays the current figure but does not take data as argument.Final Answer:
plt.imshow(img) -> Option CQuick Check:
Image display = plt.imshow() [OK]
Hint: Use imshow to display arrays as images [OK]
Common Mistakes:
- Using plt.plot for image data
- Passing data to plt.show() incorrectly
- Confusing histogram with image display
3. What will the following code display?
import matplotlib.pyplot as plt import numpy as np img = np.array([[0, 1], [1, 0]]) plt.imshow(img, cmap='gray') plt.show()
medium
Solution
Step 1: Understand the array and cmap
The array has values 0 and 1 arranged in a 2x2 grid. Usingcmap='gray'maps 0 to black and 1 to white.Step 2: Predict the image output
The image will show a 2x2 grid with black and white pixels arranged as per the array.Final Answer:
A 2x2 image with black and white pixels -> Option AQuick Check:
Array + cmap='gray' = black/white image [OK]
Hint: cmap='gray' shows 0 as black, 1 as white [OK]
Common Mistakes:
- Expecting a line plot instead of image
- Thinking cmap='gray' causes error
- Assuming image will be blank
4. Identify the error in this code snippet:
import matplotlib.pyplot as plt import numpy as np img = np.random.rand(5,5) plt.imshow(img, cmap='viridis', interpolation='none') plt.show()
medium
Solution
Step 1: Check interpolation parameter
In matplotlib, interpolation='none' is valid and means no smoothing.Step 2: Verify cmap and array creation
'viridis' is a standard colormap, and np.random.rand(5,5) creates a 5x5 array of floats between 0 and 1.Step 3: Confirm code behavior
The code runs without error and displays a 5x5 colored image with viridis colors and no interpolation smoothing.Final Answer:
The code runs without error and shows the image -> Option DQuick Check:
interpolation='none' and cmap='viridis' are valid [OK]
Hint: Check docs: 'none' is valid interpolation [OK]
Common Mistakes:
- Assuming 'none' is invalid interpolation
- Thinking 'viridis' cmap is missing
- Believing np.random.rand can't make 2D arrays
5. You have a grayscale image stored as a 2D numpy array with values from 0 to 255. You want to display it with matplotlib so that the darkest pixel is black and the brightest is white. Which code snippet achieves this correctly?
hard
Solution
Step 1: Understand grayscale display with imshow
To show grayscale correctly, usecmap='gray'and setvmin=0(black) andvmax=255(white) to map pixel values properly.Step 2: Evaluate other options
plt.imshow(image_array, cmap='gray', vmin=255, vmax=0) reverses vmin and vmax, causing inverted colors. plt.imshow(image_array, cmap='viridis', vmin=0, vmax=255) uses wrong colormap 'viridis'. plt.imshow(image_array) lacks vmin/vmax, so colors may not map correctly.Final Answer:
plt.imshow(image_array, cmap='gray', vmin=0, vmax=255) -> Option BQuick Check:
Grayscale with correct vmin/vmax = plt.imshow(image_array, cmap='gray', vmin=0, vmax=255) [OK]
Hint: Set vmin=0 and vmax=255 for correct grayscale [OK]
Common Mistakes:
- Reversing vmin and vmax values
- Using wrong colormap for grayscale
- Not setting vmin and vmax for pixel range
