0
0
Matplotlibdata~20 mins

Why image handling matters in Matplotlib - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Image Handling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this image plot code?
Consider the following Python code using matplotlib to display an image. What will be the shape of the displayed image array?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

image = np.random.rand(100, 150)
plt.imshow(image, cmap='gray')
plt.show()
print(image.shape)
A(100, 150)
B(150, 100)
C(100, 150, 3)
D(150, 100, 3)
Attempts:
2 left
💡 Hint
Remember that numpy arrays for grayscale images have 2 dimensions: height and width.
data_output
intermediate
1:30remaining
What is the pixel value at position (50, 75)?
Given this code that creates a grayscale image with a gradient, what is the pixel value at row 50, column 75?
Matplotlib
import numpy as np
image = np.tile(np.linspace(0, 1, 150), (100, 1))
print(round(image[50, 75], 2))
A0.75
B1.0
C0.25
D0.5
Attempts:
2 left
💡 Hint
The image is a horizontal gradient from 0 to 1 over 150 columns.
visualization
advanced
2:00remaining
Which plot shows the correct color mapping for this image?
You have an image array with values from 0 to 255. Which matplotlib code snippet correctly displays it as a grayscale image?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
image = np.random.randint(0, 256, (50, 50))
A
plt.imshow(image, cmap='hot')
plt.show()
B
plt.imshow(image)
plt.show()
C
plt.imshow(image, cmap='gray')
plt.show()
D
plt.imshow(image, cmap='jet')
plt.show()
Attempts:
2 left
💡 Hint
Grayscale images need the 'gray' colormap to show shades of gray.
🔧 Debug
advanced
2:30remaining
Why does this image display incorrectly?
This code tries to display a color image but the colors look wrong. What is the cause?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
image = np.random.randint(0, 256, (100, 100, 3))
plt.imshow(image)
plt.show()
AThe pixel values must be normalized between 0 and 1
BThe color channels are in BGR order instead of RGB
CThe image shape should be (3, 100, 100) not (100, 100, 3)
DThe image array dtype should be float, not int
Attempts:
2 left
💡 Hint
Matplotlib expects RGB order for color images.
🚀 Application
expert
3:00remaining
How to efficiently resize a large image array for faster plotting?
You have a large image array of shape (4000, 6000). You want to display a smaller version quickly using matplotlib. Which approach is best?
AUse numpy slicing: image_small = image[::10, ::10]
BUse plt.imshow(image, interpolation='nearest') without resizing
CUse plt.imshow(image, cmap='gray') directly on large image
DUse image_small = image.resize((400, 600))
Attempts:
2 left
💡 Hint
Reducing image size by sampling pixels speeds up plotting.