Challenge - 5 Problems
Jitter Scatter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of jittered scatter plot x-coordinates
What is the range of x-coordinates generated by the jitter in this categorical scatter plot code?
Matplotlib
import numpy as np import matplotlib.pyplot as plt categories = ['A', 'B', 'C'] values = [5, 7, 6] x = np.arange(len(categories)) jitter = (np.random.rand(len(categories)) - 0.5) * 0.2 x_jittered = x + jitter print(x_jittered)
Attempts:
2 left
💡 Hint
Consider how jitter is calculated and added to the integer positions.
✗ Incorrect
The jitter is calculated as a random number between -0.1 and 0.1 (since (rand - 0.5) * 0.2 ranges from -0.1 to 0.1). Adding this to x values 0,1,2 results in values between -0.1 and 2.1.
❓ data_output
intermediate2:00remaining
Number of points plotted with jitter
Given this code, how many points will be plotted on the scatter plot?
Matplotlib
import numpy as np import matplotlib.pyplot as plt categories = ['X', 'Y'] values = [3, 4] x = np.arange(len(categories)) jitter = (np.random.rand(len(categories)*3) - 0.5) * 0.3 x_jittered = np.repeat(x, 3) + jitter plt.scatter(x_jittered, np.tile(values, 3)) plt.show() print(len(x_jittered))
Attempts:
2 left
💡 Hint
Check how many times each category is repeated and how jitter is applied.
✗ Incorrect
Each of the 2 categories is repeated 3 times, so total points = 2 * 3 = 6. But jitter array length is 6, matching repeated x. So 6 points plotted.
❓ visualization
advanced2:00remaining
Effect of jitter on categorical scatter plot
Which option shows the correct scatter plot with jitter applied to categories 'Cat1', 'Cat2', 'Cat3' with values [10, 15, 12]?
Matplotlib
import numpy as np import matplotlib.pyplot as plt categories = ['Cat1', 'Cat2', 'Cat3'] values = [10, 15, 12] x = np.arange(len(categories)) jitter = (np.random.rand(len(categories)) - 0.5) * 0.4 x_jittered = x + jitter plt.scatter(x_jittered, values) plt.xticks(x, categories) plt.show()
Attempts:
2 left
💡 Hint
Jitter adds small horizontal noise to x positions.
✗ Incorrect
Jitter adds small random horizontal offsets to the categorical positions, so points appear spread horizontally around each category.
🔧 Debug
advanced2:00remaining
Identify error in jittered scatter plot code
What error will this code produce when run?
Matplotlib
import numpy as np import matplotlib.pyplot as plt categories = ['A', 'B', 'C'] values = [1, 2, 3] x = np.arange(len(categories)) jitter = np.random.rand(len(categories)) * 0.2 - 0.1 x_jittered = x + jitter plt.scatter(x_jittered, values) plt.xticks(x, categories) plt.show()
Attempts:
2 left
💡 Hint
Check array lengths and types used in scatter and xticks.
✗ Incorrect
x_jittered and values both have length 3, so scatter works fine. plt.xticks uses x and categories of same length, so no error.
🚀 Application
expert3:00remaining
Calculate mean y-value per category after jittered scatter
Given this jittered scatter plot data, what is the mean y-value for category 'B'?
Matplotlib
import numpy as np categories = ['A', 'B', 'C'] values = [5, 10, 15] x = np.repeat(np.arange(len(categories)), 4) jitter = (np.random.rand(len(x)) - 0.5) * 0.3 x_jittered = x + jitter # y-values repeated 4 times per category y = np.tile(values, 4) # Select y-values where x corresponds to category 'B' (index 1) y_b = y[x == 1] mean_y_b = np.mean(y_b) print(mean_y_b)
Attempts:
2 left
💡 Hint
Jitter does not affect y-values or category indexing here.
✗ Incorrect
The y-values for category 'B' are repeated 4 times as 10, so mean is 10.0. Jitter affects x_jittered but selection uses x without jitter.