0
0
NumPydata~20 mins

NumPy with Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NumPy with Matplotlib Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()
AA bar chart with bars representing sine values at discrete points
BA scatter plot of points randomly distributed between 0 and 10 on x-axis
CAn empty plot with no lines or points
DA line plot showing a sine wave oscillating between -1 and 1 over x from 0 to 10
Attempts:
2 left
💡 Hint
Think about what plt.plot does with x and y arrays.
data_output
intermediate
1: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)
A(3, 5) (3, 5)
B(5, 5) (3, 3)
C(5, 3) (5, 3)
D(3, 3) (5, 5)
Attempts:
2 left
💡 Hint
meshgrid creates coordinate matrices for vectorized evaluations.
visualization
advanced
2: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()
AA 3x3 grid with uniform color and no gradient
BA 3x3 grid with colors ranging from dark purple (low) to yellow (high) increasing from top-left to bottom-right
CA 3x3 grid with colors randomly assigned ignoring data values
DA line plot showing values 1 to 9 on y-axis
Attempts:
2 left
💡 Hint
imshow colors cells based on their numeric values using the colormap.
🔧 Debug
advanced
1: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()
ANo error, plots a line connecting points
BTypeError: unsupported operand type(s) for +: 'int' and 'str'
CValueError: x and y must have same first dimension
DIndexError: index out of bounds
Attempts:
2 left
💡 Hint
Check if x and y arrays have the same length.
🚀 Application
expert
3:00remaining
Calculate and plot the 2D Gaussian distribution
Which code snippet correctly calculates a 2D Gaussian distribution and plots it as a contour plot?
A
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(x, y)
Z = (1/(2*np.pi)) * np.exp(-0.5*(X**2 + Y**2))
plt.contour(X, Y, Z)
plt.title('2D Gaussian')
plt.show()
B
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(x, y)
Z = np.exp(-(X + Y)**2)
plt.contourf(X, Y, Z)
plt.title('2D Gaussian')
plt.show()
C
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(x, y)
Z = (1/np.sqrt(2*np.pi)) * np.exp(-0.5*(X**2 + Y**2))
plt.contour(X, Y, Z)
plt.title('2D Gaussian')
plt.show()
D
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(x, y)
Z = (1/(2*np.pi)) * np.exp(-0.5*(X + Y))
plt.contour(X, Y, Z)
plt.title('2D Gaussian')
plt.show()
Attempts:
2 left
💡 Hint
Recall the formula for 2D Gaussian: (1/(2π)) * exp(-0.5*(x² + y²))