Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a heatmap using plt.pcolormesh.
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 instead of plt.pcolormesh.
Using plt.scatter which is for scatter plots.
Using plt.bar which is for bar charts.
✗ Incorrect
The plt.pcolormesh function creates a heatmap from a 2D array.
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]]) mesh = plt.pcolormesh(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.
Using plt.grid which adds grid lines.
Using plt.title which adds a title.
✗ Incorrect
The plt.colorbar() function adds a color scale bar to the heatmap.
3fill in blank
hardFix the error in the code to correctly display the heatmap with a custom colormap.
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.array([[1, 4], [9, 16]]) plt.pcolormesh(data, cmap=[1]) plt.colorbar() plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the colormap name without quotes causes an error.
Using an invalid colormap name.
Passing None disables the colormap.
✗ Incorrect
The cmap parameter expects a string name of the colormap in quotes.
4fill in blank
hardFill both blanks to create a heatmap with labeled axes and a title.
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.array([[2, 3], [4, 5]]) plt.pcolormesh(data) plt.xlabel([1]) plt.title([2]) plt.colorbar() plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping axis labels and title text.
Forgetting to put labels in quotes.
Not adding a title at all.
✗ Incorrect
Use plt.xlabel to label the x-axis and plt.title to add a title to the plot.
5fill in blank
hardFill all three blanks to create a heatmap with custom x and y ticks and a colorbar label.
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.array([[7, 8], [9, 10]]) mesh = plt.pcolormesh(data) plt.xticks([1]) plt.yticks([2]) plt.colorbar(mesh, label=[3]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings instead of lists for ticks.
Not passing the mesh object to colorbar.
Forgetting quotes around the label.
✗ Incorrect
Use plt.xticks and plt.yticks to set tick positions. The colorbar label is a string describing the data.