Bird
Raised Fist0
Matplotlibdata~10 mins

Categorical scatter with jitter in Matplotlib - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Categorical scatter with jitter
Start with categorical data
↓
Assign numeric positions to categories
↓
Add small random noise (jitter) to positions
↓
Plot points at jittered positions
↓
Display scatter plot with spread points
We convert categories to numbers, add small random shifts to avoid overlap, then plot points spread out for clarity.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

categories = ['A', 'B', 'A', 'C', 'B']
positions = [0, 1, 0, 2, 1]
jitter = np.random.uniform(-0.1, 0.1, size=len(positions))
plt.scatter(np.array(positions) + jitter, [1]*len(positions))
plt.xticks([0,1,2], ['A','B','C'])
plt.show()
This code plots categorical points with jitter to spread overlapping points horizontally.
Execution Table
StepVariableValue/ActionResult/Effect
1categories['A', 'B', 'A', 'C', 'B']Original categorical data
2positions[0, 1, 0, 2, 1]Numeric positions assigned to categories
3jitterrandom values between -0.1 and 0.1Small noise added to positions
4positions + jittere.g. [0.05, 0.92, -0.07, 2.03, 1.08]Jittered positions for plotting
5plt.scatterplot points at jittered positionsScatter plot with spread points
6plt.xticksset x-axis labels to ['A','B','C']Categories shown on x-axis
7plt.showdisplay plotVisual output with jittered categorical scatter
💡 All points plotted with jitter to avoid overlap, plot displayed.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
categoriesNone['A', 'B', 'A', 'C', 'B']SameSameSame
positionsNone[0, 1, 0, 2, 1]SameSameSame
jitterNoneNone[-0.07, -0.08, 0.05, 0.03, 0.08]SameSame
positions + jitterNoneNoneNone[-0.07, 0.92, -0.07, 2.03, 1.08]Same
Key Moments - 3 Insights
Why do we add jitter to the numeric positions?
Adding jitter spreads points horizontally so they don't overlap exactly, making each point visible. See execution_table step 4 where jitter is added.
How do categories become numbers for plotting?
Each category is assigned a number (like 0 for 'A', 1 for 'B', 2 for 'C') so matplotlib can plot them on a numeric axis. See execution_table step 2.
Why do we set x-axis ticks after plotting?
Because positions are numbers, we set x-axis labels to show category names for clarity. See execution_table step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the variable_tracker table, what is the value of 'positions + jitter' after step 4?
A[0, 1, 0, 2, 1]
B['A', 'B', 'A', 'C', 'B']
C[-0.07, 0.92, 0.05, 2.03, 1.08]
D[-0.1, 0.1, -0.1, 0.1, 0.1]
💡 Hint
Check the 'positions + jitter' row in variable_tracker after step 4.
At which step in the execution_table do we assign numeric positions to categories?
AStep 2
BStep 4
CStep 1
DStep 6
💡 Hint
Look for the step where 'positions' variable is set.
If jitter range was increased to (-0.5, 0.5), how would the scatter plot points change?
APoints would overlap more
BPoints would be more spread out horizontally
CPoints would move vertically
DPoints would disappear
💡 Hint
Jitter controls horizontal noise added to positions, see execution_table step 3.
Concept Snapshot
Categorical scatter with jitter:
- Map categories to numbers (e.g., A=0, B=1)
- Add small random noise (jitter) to numeric positions
- Plot points at jittered positions to avoid overlap
- Set x-axis ticks to category labels
- Result: clear scatter plot showing category distribution
Full Transcript
This visual execution shows how to plot categorical data points with jitter using matplotlib. First, categories are converted to numeric positions. Then, small random noise called jitter is added to these positions to spread points horizontally and avoid overlap. The jittered positions are plotted as a scatter plot. Finally, x-axis ticks are set to show category names for clarity. This method helps visualize overlapping categorical points clearly.

Practice

(1/5)
1. What is the main purpose of adding jitter in a categorical scatter plot using matplotlib?
easy
A. To increase the size of the points
B. To change the color of the points randomly
C. To spread overlapping points so they are easier to see
D. To remove outliers from the data

Solution

  1. Step 1: Understand what jitter does

    Jitter adds small random noise to the position of points on the categorical axis.
  2. Step 2: Effect of jitter on overlapping points

    This noise spreads points that would overlap, making each point visible.
  3. Final Answer:

    To spread overlapping points so they are easier to see -> Option C
  4. Quick Check:

    Jitter spreads points = A [OK]
Hint: Jitter spreads points to avoid overlap [OK]
Common Mistakes:
  • Thinking jitter changes point color
  • Assuming jitter increases point size
  • Believing jitter removes data points
2. Which of the following matplotlib code snippets correctly adds jitter to a categorical scatter plot on the x-axis?
easy
A. plt.scatter(x + np.random.uniform(-0.1, 0.1, size=len(x)), y)
B. plt.scatter(x * np.random.uniform(-0.1, 0.1, size=len(x)), y)
C. plt.scatter(x - np.random.randint(1, 5, size=len(x)), y)
D. plt.scatter(x / np.random.uniform(0.1, 1, size=len(x)), y)

Solution

  1. Step 1: Identify correct jitter addition

    Jitter is added by adding small random noise to the x values.
  2. 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.
  3. Final Answer:

    plt.scatter(x + np.random.uniform(-0.1, 0.1, size=len(x)), y) -> Option A
  4. Quick Check:

    Add small noise = C [OK]
Hint: Add small uniform noise to x for jitter [OK]
Common Mistakes:
  • Multiplying x by noise instead of adding
  • Subtracting large integers causing big shifts
  • Dividing x by noise changing scale
3. What will be the effect of the following code on the scatter plot?
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()
medium
A. Points will be stacked vertically at each category
B. Plot will show an error due to jitter size mismatch
C. Points will be plotted exactly at integer x positions
D. Points will be spread horizontally around each category label

Solution

  1. Step 1: Understand jitter addition

    Jitter adds random noise between -0.2 and 0.2 to x positions, spreading points horizontally.
  2. 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.
  3. Final Answer:

    Points will be spread horizontally around each category label -> Option D
  4. Quick Check:

    Jitter shifts points horizontally = B [OK]
Hint: Jitter shifts points horizontally near categories [OK]
Common Mistakes:
  • Thinking jitter stacks points vertically
  • Assuming points stay exactly at integers
  • Believing code causes error due to jitter size
4. The following code is intended to add jitter to a categorical scatter plot but does not work as expected. What is the main error?
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()
medium
A. Jitter is a single number, not an array matching x length
B. x and y arrays have different lengths
C. plt.xticks labels do not match x values
D. Random uniform range is too large causing overlap

Solution

  1. Step 1: Check jitter variable

    Jitter is created as a single float, not an array matching x length.
  2. Step 2: Effect on scatter

    Adding a single float to an array works but adds same jitter to all points, no spread.
  3. Final Answer:

    Jitter is a single number, not an array matching x length -> Option A
  4. Quick Check:

    Jitter must be array for point-wise noise = A [OK]
Hint: Jitter must be array same size as data [OK]
Common Mistakes:
  • Assuming single jitter value adds spread
  • Ignoring length mismatch of jitter and x
  • Thinking xticks labels cause error
5. You have a dataset with categories 'Red', 'Green', 'Blue' and multiple points per category. You want to create a categorical scatter plot with jitter to visualize all points clearly. Which approach is best to implement jitter correctly in matplotlib?
hard
A. Add jitter by multiplying category integers by random values greater than 1
B. Map categories to integers, add small random noise array to these integers, then plot with category labels on x-axis
C. Use bar plot instead of scatter plot to avoid jitter
D. Plot categories as strings on x-axis and add jitter by changing y values randomly

Solution

  1. Step 1: Map categories to integers

    Convert categorical labels to integer positions for plotting on x-axis.
  2. Step 2: Add small random noise array to integers

    Add jitter by adding a small random noise array to spread points horizontally.
  3. Step 3: Plot with category labels

    Use plt.xticks to label integer positions with original category names.
  4. Final Answer:

    Map categories to integers, add small random noise array to these integers, then plot with category labels on x-axis -> Option B
  5. Quick Check:

    Integer mapping + noise + labels = D [OK]
Hint: Map categories to numbers, add noise, label ticks [OK]
Common Mistakes:
  • Adding jitter to y instead of x
  • Using bar plot which doesn't show jitter
  • Multiplying integers causing large shifts
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard