Challenge - 5 Problems
Heatmap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Remember that in grayscale, lower values are darker and higher values are brighter.
✗ Incorrect
The array values increase from 1 to 4 from top-left to bottom-right. Using 'gray' colormap, smaller values appear darker and larger values brighter, so the heatmap shows a gradient from dark (top-left) to bright (bottom-right).
❓ data_output
intermediate1: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()
Attempts:
2 left
💡 Hint
Check the shape of the numpy array before plotting.
✗ Incorrect
The array 'data' is created with shape (5, 3), so plt.imshow displays a heatmap with 5 rows and 3 columns.
❓ visualization
advanced2: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()
Attempts:
2 left
💡 Hint
Aspect controls the ratio of height to width of the heatmap cells.
✗ Incorrect
By default, plt.imshow uses aspect='equal' which keeps cells square. Setting aspect='auto' lets cells stretch to fill the axes, changing their shape to rectangles if the axes shape is not square.
🔧 Debug
advanced1: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()
Attempts:
2 left
💡 Hint
Check the shape of the array passed to plt.imshow.
✗ Incorrect
plt.imshow requires a 2D array to display a heatmap. Passing a 1D array causes a ValueError indicating the input must be 2D.
🚀 Application
expert3: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()
Attempts:
2 left
💡 Hint
Look at how the colormap maps values from 0 to 1 to colors.
✗ Incorrect
The colormap maps the lowest value (0) to blue, middle value (1.5) to white, and highest value (3) normalized to 1 to red. So the highest value appears red.