0
0
Matplotlibdata~20 mins

Heatmap with plt.pcolormesh in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Heatmap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output of plt.pcolormesh with given data
What will be the shape of the array used to create the heatmap with plt.pcolormesh in this code?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt

Z = np.array([[1, 2, 3], [4, 5, 6]])
plt.pcolormesh(Z)
plt.close()  # Close plot to avoid display
print(Z.shape)
A(2, 2)
B(3, 2)
C(3, 3)
D(2, 3)
Attempts:
2 left
💡 Hint
Remember that the shape of the array Z is rows by columns.
data_output
intermediate
1:00remaining
Data values shown by plt.pcolormesh
Given this code, what is the color value at position (1, 1) in the heatmap data array?
Matplotlib
import numpy as np
Z = np.array([[10, 20, 30], [40, 50, 60]])
value = Z[1, 1]
print(value)
A50
B40
C20
D60
Attempts:
2 left
💡 Hint
Remember that indexing starts at 0 for rows and columns.
visualization
advanced
2:00remaining
Effect of shading='auto' in plt.pcolormesh
What visual difference does setting shading='auto' make in plt.pcolormesh when plotting a 2x2 array?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt

Z = np.array([[1, 2], [3, 4]])
plt.figure(figsize=(4, 3))
plt.pcolormesh(Z, shading='auto')
plt.close()
AThe heatmap cells have gaps between them.
BThe heatmap cells align exactly with the data points without gaps.
CThe heatmap uses a different color map automatically.
DThe heatmap cells are plotted as circles instead of squares.
Attempts:
2 left
💡 Hint
shading='auto' adjusts the grid to match the data shape.
🔧 Debug
advanced
1:30remaining
Identify the error in plt.pcolormesh usage
What error will this code raise when run?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt

Z = np.array([[1, 2], [3, 4]])
plt.pcolormesh(Z, cmap='viridis', shading='invalid')
plt.close()
AValueError: shading must be 'flat', 'gouraud', or 'auto'
BTypeError: cmap must be a Colormap instance
CSyntaxError: invalid syntax in plt.pcolormesh call
DNo error, plot displays normally
Attempts:
2 left
💡 Hint
Check the allowed values for the shading parameter.
🚀 Application
expert
2:30remaining
Creating a heatmap with custom X and Y coordinates
Which option correctly creates a heatmap with plt.pcolormesh using custom X and Y coordinates for a 2x2 data array?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt

Z = np.array([[1, 2], [3, 4]])
X = np.array([0, 1, 2])
Y = np.array([0, 1, 2])
# Fill in the correct plt.pcolormesh call below
A
plt.pcolormesh(Z, X, Y)
plt.close()
B
plt.pcolormesh(X, Y, Z.T)
plt.close()
C
plt.pcolormesh(X, Y, Z)
plt.close()
D
plt.pcolormesh(Z.T, X, Y)
plt.close()
Attempts:
2 left
💡 Hint
X and Y must be 1D arrays with length one more than Z's dimensions.