Bird
Raised Fist0
Matplotlibdata~20 mins

Displaying images with imshow in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Image Display Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What does this imshow code display?

Consider this Python code using matplotlib to display an image:

import matplotlib.pyplot as plt
import numpy as np

image = np.array([[0, 1], [2, 3]])
plt.imshow(image, cmap='gray')
plt.colorbar()
plt.show()

What will the displayed image look like?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

image = np.array([[0, 1], [2, 3]])
plt.imshow(image, cmap='gray')
plt.colorbar()
plt.show()
AA 2x2 grayscale image with all pixels the same shade of gray
BA 2x2 grayscale image with the darkest pixel at top-left and brightest at bottom-right
CAn error because the image array is not 3-dimensional
DA 2x2 color image with random colors
Attempts:
2 left
💡 Hint

Think about how imshow maps numbers to colors using the grayscale colormap.

data_output
intermediate
1:30remaining
What is the shape of the image shown?

Given this code snippet:

import matplotlib.pyplot as plt
import numpy as np

image = np.random.rand(100, 150, 3)
plt.imshow(image)
plt.show()

What is the shape of the image array being displayed?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

image = np.random.rand(100, 150, 3)
plt.imshow(image)
plt.show()
A(100, 150, 3)
B(150, 100, 3)
C(100, 150)
D(3, 100, 150)
Attempts:
2 left
💡 Hint

Remember the shape format for color images is (height, width, color_channels).

visualization
advanced
2:30remaining
Which option produces a heatmap with a colorbar?

Which code snippet correctly displays a 10x10 heatmap with a colorbar using imshow?

A
import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(10,10)
plt.imshow(data, cmap='hot')
plt.colorbar(data)
plt.show()
B
import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(10,10)
plt.imshow(data)
plt.colorbar('hot')
plt.show()
C
import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(10,10)
plt.imshow(data, cmap='hot')
plt.colorbar(cmap='hot')
plt.show()
D
import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(10,10)
plt.imshow(data, cmap='hot')
plt.colorbar()
plt.show()
Attempts:
2 left
💡 Hint

Check how colorbar() is called and how colormaps are assigned.

🔧 Debug
advanced
2:00remaining
What error does this code raise?

Analyze this code snippet:

import matplotlib.pyplot as plt
import numpy as np

image = np.array([1, 2, 3, 4])
plt.imshow(image)
plt.show()

What error will occur when running this?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

image = np.array([1, 2, 3, 4])
plt.imshow(image)
plt.show()
ANo error, displays a 1D image
BTypeError: Image data must be 2D or 3D array
CValueError: Invalid shape for image data
DIndexError: index out of bounds
Attempts:
2 left
💡 Hint

Check the shape requirements for imshow input arrays.

🚀 Application
expert
3:00remaining
How to display an RGB image with transparency using imshow?

You have an RGB image stored as a NumPy array img with shape (100, 100, 3). You want to display it with 50% transparency over a white background using imshow. Which code snippet achieves this?

A
plt.imshow(img, alpha=0.5)
plt.gca().set_facecolor('white')
plt.show()
B
plt.imshow(img)
plt.alpha(0.5)
plt.show()
C
plt.imshow(img, transparency=0.5)
plt.show()
D
plt.imshow(img, alpha=0.5, background='white')
plt.show()
Attempts:
2 left
💡 Hint

Check how to set transparency and background color in matplotlib.

Practice

(1/5)
1. What does the imshow function in matplotlib do?
easy
A. Displays image data as a picture
B. Creates a line plot from data points
C. Generates a histogram of values
D. Saves an image file to disk

Solution

  1. Step 1: Understand the purpose of imshow

    imshow is designed to display image data visually as a picture.
  2. Step 2: Compare with other plotting functions

    Other functions like line plots or histograms serve different purposes, so they don't match imshow's role.
  3. Final Answer:

    Displays image data as a picture -> Option A
  4. Quick 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
A. plt.hist(img)
B. plt.plot(img)
C. plt.imshow(img)
D. plt.show(img)

Solution

  1. Step 1: Identify the function to display images

    To show an image from a 2D array, plt.imshow() is the correct function.
  2. Step 2: Check other options for correctness

    plt.plot() is for line plots, plt.hist() for histograms, and plt.show() displays the current figure but does not take data as argument.
  3. Final Answer:

    plt.imshow(img) -> Option C
  4. Quick 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
A. A 2x2 image with black and white pixels
B. A line plot of the array values
C. An error because cmap='gray' is invalid
D. A blank plot with no image

Solution

  1. Step 1: Understand the array and cmap

    The array has values 0 and 1 arranged in a 2x2 grid. Using cmap='gray' maps 0 to black and 1 to white.
  2. Step 2: Predict the image output

    The image will show a 2x2 grid with black and white pixels arranged as per the array.
  3. Final Answer:

    A 2x2 image with black and white pixels -> Option A
  4. Quick 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
A. The interpolation value 'none' is invalid
B. The cmap 'viridis' does not exist
C. np.random.rand cannot create 2D arrays
D. The code runs without error and shows the image

Solution

  1. Step 1: Check interpolation parameter

    In matplotlib, interpolation='none' is valid and means no smoothing.
  2. 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.
  3. Step 3: Confirm code behavior

    The code runs without error and displays a 5x5 colored image with viridis colors and no interpolation smoothing.
  4. Final Answer:

    The code runs without error and shows the image -> Option D
  5. Quick 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
A. plt.imshow(image_array, cmap='viridis', vmin=0, vmax=255)
B. plt.imshow(image_array, cmap='gray', vmin=0, vmax=255)
C. plt.imshow(image_array, cmap='gray', vmin=255, vmax=0)
D. plt.imshow(image_array)

Solution

  1. Step 1: Understand grayscale display with imshow

    To show grayscale correctly, use cmap='gray' and set vmin=0 (black) and vmax=255 (white) to map pixel values properly.
  2. 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.
  3. Final Answer:

    plt.imshow(image_array, cmap='gray', vmin=0, vmax=255) -> Option B
  4. Quick 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