0
0
Matplotlibdata~20 mins

Heatmap with plt.imshow in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Heatmap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of plt.imshow with a simple 2D array
What will be the color intensity pattern shown by plt.imshow for this 2D array?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

array = np.array([[1, 2], [3, 4]])
plt.imshow(array, cmap='gray')
plt.colorbar()
plt.show()
AA heatmap with the top-left corner darkest and bottom-right corner brightest
BA heatmap with uniform color intensity across all cells
CA heatmap with the top-left corner brightest and bottom-right corner darkest
DA heatmap showing only two colors alternating in a checkerboard pattern
Attempts:
2 left
💡 Hint
Remember that in grayscale, lower values are darker and higher values are brighter.
data_output
intermediate
1:30remaining
Shape of the array displayed by plt.imshow
Given this code, what is the shape of the array that plt.imshow displays as a heatmap?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt

data = np.random.rand(5, 3)
plt.imshow(data)
plt.show()
A(5, 3)
B(3, 5)
C(5, 5)
D(3, 3)
Attempts:
2 left
💡 Hint
Check the shape of the numpy array before plotting.
visualization
advanced
2:30remaining
Effect of aspect parameter in plt.imshow
What visual effect does setting aspect='auto' have on the heatmap created by plt.imshow compared to the default aspect setting?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt

matrix = np.arange(12).reshape(3,4)
plt.subplot(1,2,1)
plt.title('Default aspect')
plt.imshow(matrix)
plt.subplot(1,2,2)
plt.title('aspect="auto"')
plt.imshow(matrix, aspect='auto')
plt.show()
AThe heatmap disappears when aspect='auto' is used.
BThe heatmap cells remain perfect squares in both plots.
CThe heatmap colors invert when aspect='auto' is set.
DThe heatmap cells stretch to fill the plot area, changing their shape from square to rectangles.
Attempts:
2 left
💡 Hint
Aspect controls the ratio of height to width of the heatmap cells.
🔧 Debug
advanced
1:30remaining
Identify the error in this plt.imshow heatmap code
What error will this code produce when run?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

arr = np.array([1, 2, 3, 4])
plt.imshow(arr)
plt.show()
ANo error, heatmap displays correctly
BTypeError: Unsupported data type for imshow
CValueError: Input must be 2D array, got 1D array instead
DIndexError: index out of bounds
Attempts:
2 left
💡 Hint
Check the shape of the array passed to plt.imshow.
🚀 Application
expert
3:00remaining
Interpreting a heatmap with plt.imshow and custom colormap
You create a heatmap with plt.imshow using this code. What color corresponds to the highest value in the data?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

values = np.array([[0, 1], [2, 3]])
colors = [(0, 'blue'), (0.5, 'white'), (1, 'red')]
custom_cmap = LinearSegmentedColormap.from_list('custom', colors)
plt.imshow(values, cmap=custom_cmap)
plt.colorbar()
plt.show()
AWhite
BRed
CBlue
DBlack
Attempts:
2 left
💡 Hint
Look at how the colormap maps values from 0 to 1 to colors.