0
0
Matplotlibdata~10 mins

Heatmap with plt.imshow 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 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'
Ascatter
Bplot
Cimshow
Dbar
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.plot() which is for line plots.
Using plt.scatter() which is for scatter plots.
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]])
plt.imshow(data)
plt.[1]()
plt.show()
Drag options to blanks, or click blank then click option'
Alegend
Bcolorbar
Ctitle
Dxlabel
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.legend() which is for plot legends.
Trying to use plt.title() to add color scale.
3fill in blank
hard

Fix 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'
A'cool'
B'gray'
C'spring'
D'hot'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the colormap name in quotes.
Using a colormap that doesn't highlight heat well.
4fill in blank
hard

Fill 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'
A40
B30
C'viridis'
D'plasma'
Attempts:
3 left
💡 Hint
Common Mistakes
Dividing by a wrong number that is not the max value.
Using a colormap name without quotes.
5fill in blank
hard

Fill 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'
A'bilinear'
B'nearest'
C'viridis'
D'My Heatmap'
Attempts:
3 left
💡 Hint
Common Mistakes
Using interpolation values without quotes.
Forgetting quotes around the title string.