Categorical scatter with jitter in Matplotlib - Time & Space Complexity
We want to understand how the time to draw a categorical scatter plot with jitter changes as we add more data points.
How does the plotting time grow when the number of points increases?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
import numpy as np
categories = ['A', 'B', 'C']
n = 1000
x = np.random.choice(categories, n)
y = np.random.randn(n)
jitter = (np.random.rand(n) - 0.5) * 0.1
plt.scatter([categories.index(cat) + jitter[i] for i, cat in enumerate(x)], y)
plt.show()
This code plots points for categories A, B, and C with a small random shift (jitter) to avoid overlap.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over all data points to assign jitter and plot each point.
- How many times: Once for each of the n points.
Each additional point adds a small fixed amount of work to place and draw it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 point placements and draws |
| 100 | About 100 point placements and draws |
| 1000 | About 1000 point placements and draws |
Pattern observation: The work grows directly with the number of points.
Time Complexity: O(n)
This means the time to create the plot grows in a straight line as you add more points.
[X] Wrong: "Adding jitter makes the plot take much longer because it's more complex than just plotting points."
[OK] Correct: The jitter is just a small calculation per point and does not add extra loops or nested work, so it does not change the overall growth pattern.
Understanding how plotting time grows helps you handle larger datasets smoothly and shows you can think about performance even in visualization tasks.
"What if we added a nested loop to compare each point to every other point for overlap detection? How would the time complexity change?"
Practice
matplotlib?Solution
Step 1: Understand what jitter does
Jitter adds small random noise to the position of points on the categorical axis.Step 2: Effect of jitter on overlapping points
This noise spreads points that would overlap, making each point visible.Final Answer:
To spread overlapping points so they are easier to see -> Option CQuick Check:
Jitter spreads points = A [OK]
- Thinking jitter changes point color
- Assuming jitter increases point size
- Believing jitter removes data points
matplotlib code snippets correctly adds jitter to a categorical scatter plot on the x-axis?Solution
Step 1: Identify correct jitter addition
Jitter is added by adding small random noise to the x values.Step 2: Check each option
plt.scatter(x + np.random.uniform(-0.1, 0.1, size=len(x)), y) adds small uniform noise between -0.1 and 0.1, which is correct jitter. Others multiply, subtract large integers, or divide, which distort data incorrectly.Final Answer:
plt.scatter(x + np.random.uniform(-0.1, 0.1, size=len(x)), y) -> Option AQuick Check:
Add small noise = C [OK]
- Multiplying x by noise instead of adding
- Subtracting large integers causing big shifts
- Dividing x by noise changing scale
import matplotlib.pyplot as plt import numpy as np categories = ['A', 'B', 'C'] values = [5, 7, 6] x = np.array([0, 1, 2]) jitter = np.random.uniform(-0.2, 0.2, size=3) plt.scatter(x + jitter, values) plt.xticks(x, categories) plt.show()
Solution
Step 1: Understand jitter addition
Jitter adds random noise between -0.2 and 0.2 to x positions, spreading points horizontally.Step 2: Effect on scatter plot
Points will no longer be exactly at 0,1,2 but slightly shifted, making them spread horizontally around category labels.Final Answer:
Points will be spread horizontally around each category label -> Option DQuick Check:
Jitter shifts points horizontally = B [OK]
- Thinking jitter stacks points vertically
- Assuming points stay exactly at integers
- Believing code causes error due to jitter size
import matplotlib.pyplot as plt import numpy as np x = np.array([0, 1, 2, 0, 1, 2]) y = np.array([5, 6, 7, 5, 6, 7]) jitter = np.random.uniform(-0.1, 0.1) plt.scatter(x + jitter, y) plt.xticks([0, 1, 2], ['A', 'B', 'C']) plt.show()
Solution
Step 1: Check jitter variable
Jitter is created as a single float, not an array matching x length.Step 2: Effect on scatter
Adding a single float to an array works but adds same jitter to all points, no spread.Final Answer:
Jitter is a single number, not an array matching x length -> Option AQuick Check:
Jitter must be array for point-wise noise = A [OK]
- Assuming single jitter value adds spread
- Ignoring length mismatch of jitter and x
- Thinking xticks labels cause error
matplotlib?Solution
Step 1: Map categories to integers
Convert categorical labels to integer positions for plotting on x-axis.Step 2: Add small random noise array to integers
Add jitter by adding a small random noise array to spread points horizontally.Step 3: Plot with category labels
Use plt.xticks to label integer positions with original category names.Final Answer:
Map categories to integers, add small random noise array to these integers, then plot with category labels on x-axis -> Option BQuick Check:
Integer mapping + noise + labels = D [OK]
- Adding jitter to y instead of x
- Using bar plot which doesn't show jitter
- Multiplying integers causing large shifts
Ready to prove it?
Pass the test and this pattern moves unknown
