0
0
Matplotlibdata~20 mins

Categorical scatter with jitter in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Jitter Scatter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
AValues between -0.1 and 2.1
BValues between 0 and 2
CValues between -0.5 and 2.5
DValues between 0.5 and 2.5
Attempts:
2 left
💡 Hint
Consider how jitter is calculated and added to the integer positions.
data_output
intermediate
2: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))
A9
B3
C2
D6
Attempts:
2 left
💡 Hint
Check how many times each category is repeated and how jitter is applied.
visualization
advanced
2: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()
AScatter points spread horizontally around 0,1,2 with small random offsets
BScatter points randomly scattered with no relation to categories
CScatter points spread vertically with no horizontal jitter
DScatter points aligned exactly at 0,1,2 on x-axis with no horizontal spread
Attempts:
2 left
💡 Hint
Jitter adds small horizontal noise to x positions.
🔧 Debug
advanced
2: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()
ATypeError due to adding list and numpy array
BValueError due to mismatched array lengths in scatter
CNo error, plot displays correctly
DIndexError from plt.xticks
Attempts:
2 left
💡 Hint
Check array lengths and types used in scatter and xticks.
🚀 Application
expert
3: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)
A7.5
B10.0
C12.5
DCannot determine due to jitter
Attempts:
2 left
💡 Hint
Jitter does not affect y-values or category indexing here.