We use categorical scatter plots with jitter to show data points for categories clearly. Jitter adds small random shifts so points don't overlap.
Categorical scatter with jitter in Matplotlib
import matplotlib.pyplot as plt import numpy as np categories = ['A', 'B', 'C'] values = [5, 7, 6, 8, 5, 7, 6, 9, 5] category_labels = ['A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'C'] # Convert categories to numbers x = [categories.index(cat) for cat in category_labels] # Add jitter jitter = np.random.uniform(-0.1, 0.1, size=len(x)) x_jittered = x + jitter plt.scatter(x_jittered, values) plt.xticks(range(len(categories)), categories) plt.xlabel('Category') plt.ylabel('Value') plt.title('Categorical scatter with jitter') plt.show()
Jitter is a small random number added to category positions to avoid overlap.
Categories are converted to numbers because scatter needs numeric x values.
import matplotlib.pyplot as plt import numpy as np categories = ['X', 'Y'] values = [1, 2, 3, 4, 5, 6] category_labels = ['X', 'X', 'Y', 'Y', 'Y', 'X'] x = [categories.index(cat) for cat in category_labels] jitter = np.random.uniform(-0.05, 0.05, len(x)) x_jittered = x + jitter plt.scatter(x_jittered, values) plt.xticks(range(len(categories)), categories) plt.show()
import matplotlib.pyplot as plt import numpy as np cats = ['Dog', 'Cat', 'Bird'] vals = [3, 5, 2, 4, 6, 7, 3, 5] labels = ['Dog', 'Dog', 'Cat', 'Cat', 'Bird', 'Bird', 'Bird', 'Dog'] x = [cats.index(c) for c in labels] jitter = np.random.uniform(-0.2, 0.2, len(x)) x_jittered = x + jitter plt.scatter(x_jittered, vals, color='green') plt.xticks(range(len(cats)), cats) plt.title('Pets values with jitter') plt.show()
This program shows how to plot values for color categories with jitter to avoid overlapping points. The jitter is random but fixed by seed for consistent results.
import matplotlib.pyplot as plt import numpy as np # Define categories and values categories = ['Red', 'Blue', 'Green'] values = [10, 15, 10, 20, 25, 15, 10, 30, 20] category_labels = ['Red', 'Red', 'Blue', 'Blue', 'Blue', 'Green', 'Green', 'Green', 'Green'] # Convert categories to numeric positions x = [categories.index(cat) for cat in category_labels] # Add jitter to x positions np.random.seed(42) # For reproducible jitter jitter = np.random.uniform(-0.15, 0.15, size=len(x)) x_jittered = x + jitter # Create scatter plot plt.scatter(x_jittered, values, color='purple') plt.xticks(range(len(categories)), categories) plt.xlabel('Color') plt.ylabel('Value') plt.title('Categorical scatter plot with jitter') plt.grid(True, linestyle='--', alpha=0.5) plt.show()
Use a small jitter range to keep points near their category.
Setting a random seed helps get the same jitter every time you run the code.
Jitter only affects horizontal position for categorical scatter plots.
Categorical scatter plots show individual points for categories.
Jitter adds small random shifts to avoid overlapping points.
This helps visualize data distribution within categories clearly.
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
