0
0
Matplotlibdata~10 mins

Heatmap with plt.pcolormesh in Matplotlib - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aplot
Bscatter
Cpcolormesh
Dbar
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.
2fill in blank
medium

Complete 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'
Acolorbar
Blegend
Cgrid
Dtitle
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.
3fill in blank
hard

Fix 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'
ANone
Bviridis
C'blue'
D'viridis'
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.
4fill in blank
hard

Fill 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'
A'X axis'
B'Y axis'
C'My Heatmap'
D'Data Values'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping axis labels and title text.
Forgetting to put labels in quotes.
Not adding a title at all.
5fill in blank
hard

Fill 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'
A[0, 1]
C'Intensity'
D[1, 2]
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.