Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to display the heatmap using plt.imshow.
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.array([[1, 2], [3, 4]]) plt.[1](data) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.plot() which is for line plots.
Using plt.scatter() which is for scatter plots.
✗ Incorrect
plt.imshow() is used to display data as an image, which is perfect for heatmaps.
2fill in blank
mediumComplete the code to add a colorbar to the heatmap.
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.array([[5, 10], [15, 20]]) plt.imshow(data) plt.[1]() plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.legend() which is for plot legends.
Trying to use plt.title() to add color scale.
✗ Incorrect
plt.colorbar() adds a color scale bar to the heatmap to show value mapping.
3fill in blank
hardFix the error in the code to correctly display the heatmap with a 'hot' colormap.
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.array([[7, 8], [9, 10]]) plt.imshow(data, cmap=[1]) plt.colorbar() plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the colormap name in quotes.
Using a colormap that doesn't highlight heat well.
✗ Incorrect
The 'hot' colormap shows colors from black to red to yellow to white, good for heatmaps.
4fill in blank
hardFill both blanks to create a heatmap with data normalized between 0 and 1 and use the 'viridis' colormap.
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.array([[10, 20], [30, 40]]) norm_data = data / [1] plt.imshow(norm_data, cmap=[2]) plt.colorbar() plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Dividing by a wrong number that is not the max value.
Using a colormap name without quotes.
✗ Incorrect
Dividing by 40 normalizes data to max 1; 'viridis' is a popular colormap for heatmaps.
5fill in blank
hardFill all three blanks to create a heatmap from a 3x3 matrix, set interpolation to 'nearest', and add a title 'My Heatmap'.
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) plt.imshow(data, interpolation=[1], cmap=[2]) plt.colorbar() plt.title([3]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using interpolation values without quotes.
Forgetting quotes around the title string.
✗ Incorrect
Interpolation 'nearest' shows sharp blocks; 'viridis' is a good colormap; title is a string.