Challenge - 5 Problems
Heatmap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1: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)
Attempts:
2 left
💡 Hint
Remember that the shape of the array Z is rows by columns.
✗ Incorrect
The array Z has 2 rows and 3 columns, so its shape is (2, 3). This shape is used by plt.pcolormesh to create the heatmap grid.
❓ data_output
intermediate1: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)
Attempts:
2 left
💡 Hint
Remember that indexing starts at 0 for rows and columns.
✗ Incorrect
Z[1, 1] accesses the second row and second column, which is 50.
❓ visualization
advanced2: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()
Attempts:
2 left
💡 Hint
shading='auto' adjusts the grid to match the data shape.
✗ Incorrect
Using shading='auto' makes the cells align perfectly with the data points, avoiding gaps or overlaps.
🔧 Debug
advanced1: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()
Attempts:
2 left
💡 Hint
Check the allowed values for the shading parameter.
✗ Incorrect
The shading parameter only accepts 'flat', 'gouraud', or 'auto'. 'invalid' causes a ValueError.
🚀 Application
expert2: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
Attempts:
2 left
💡 Hint
X and Y must be 1D arrays with length one more than Z's dimensions.
✗ Incorrect
X and Y define the grid edges and must have length one more than Z's shape. Z is passed as is without transpose.