0
0
Matplotlibdata~20 mins

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

Choose your learning style9 modes available
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.