Challenge - 5 Problems
NumPy with Matplotlib Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of NumPy array plot with Matplotlib
What will be the output plot when running the following code?
NumPy
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 10, 100) y = np.sin(x) plt.plot(x, y) plt.title('Sine Wave') plt.xlabel('x values') plt.ylabel('sin(x)') plt.show()
Attempts:
2 left
💡 Hint
Think about what plt.plot does with x and y arrays.
✗ Incorrect
The code creates 100 points from 0 to 10, computes their sine, and plots a continuous line showing the sine wave.
❓ data_output
intermediate1:30remaining
Shape of NumPy array after meshgrid for plotting
Given the code below, what is the shape of X and Y arrays used for contour plotting?
NumPy
import numpy as np x = np.linspace(-1, 1, 5) y = np.linspace(-1, 1, 3) X, Y = np.meshgrid(x, y) print(X.shape, Y.shape)
Attempts:
2 left
💡 Hint
meshgrid creates coordinate matrices for vectorized evaluations.
✗ Incorrect
meshgrid with x length 5 and y length 3 creates X and Y arrays with shape (3,5) matching y rows and x columns.
❓ visualization
advanced2:30remaining
Identify the correct heatmap from NumPy data
Which option shows the correct heatmap visualization for the following data array?
NumPy
import numpy as np import matplotlib.pyplot as plt data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) plt.imshow(data, cmap='viridis') plt.colorbar() plt.show()
Attempts:
2 left
💡 Hint
imshow colors cells based on their numeric values using the colormap.
✗ Incorrect
imshow with 'viridis' colors low values dark purple and high values yellow, so the heatmap shows increasing color from 1 to 9.
🔧 Debug
advanced1:30remaining
Error in plotting NumPy array with Matplotlib
What error will this code produce and why?
NumPy
import numpy as np import matplotlib.pyplot as plt x = np.array([1, 2, 3]) y = np.array([4, 5]) plt.plot(x, y) plt.show()
Attempts:
2 left
💡 Hint
Check if x and y arrays have the same length.
✗ Incorrect
Matplotlib requires x and y arrays to have the same length to plot points; here y has length 2 but x has length 3.
🚀 Application
expert3:00remaining
Calculate and plot the 2D Gaussian distribution
Which code snippet correctly calculates a 2D Gaussian distribution and plots it as a contour plot?
Attempts:
2 left
💡 Hint
Recall the formula for 2D Gaussian: (1/(2π)) * exp(-0.5*(x² + y²))
✗ Incorrect
Option A correctly implements the 2D Gaussian formula with squared terms inside the exponent and proper normalization factor.