0
0
Matplotlibdata~30 mins

Categorical scatter with jitter in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Categorical scatter with jitter
📖 Scenario: You work in a small bakery and want to analyze how many cookies of different types are sold each day. You have the sales data for three types of cookies over a week.
🎯 Goal: Create a scatter plot that shows the number of cookies sold for each cookie type. Add jitter to the x-axis positions so the points don't overlap and are easier to see.
📋 What You'll Learn
Create a dictionary called cookie_sales with keys 'Chocolate', 'Vanilla', and 'Strawberry' and values as lists of daily sales numbers.
Create a variable called jitter_strength to control the amount of jitter on the x-axis.
Use a for loop with variables cookie and sales to iterate over cookie_sales.items().
Inside the loop, create a list x_positions with jittered x-axis values for each sale.
Plot the scatter points using plt.scatter(x_positions, sales).
Print the plot with plt.show().
💡 Why This Matters
🌍 Real World
Jittered scatter plots help visualize overlapping data points in categories, useful in sales, biology, and social sciences.
💼 Career
Data analysts and scientists often use jitter plots to explore and present categorical data distributions clearly.
Progress0 / 4 steps
1
Create the cookie sales data
Create a dictionary called cookie_sales with these exact entries: 'Chocolate': [20, 22, 19, 24, 20, 23, 21], 'Vanilla': [15, 17, 14, 16, 18, 15, 17], and 'Strawberry': [10, 12, 11, 13, 12, 14, 13].
Matplotlib
Need a hint?

Use curly braces {} to create the dictionary and colons : to assign lists to each cookie type.

2
Set the jitter strength
Create a variable called jitter_strength and set it to 0.1 to control the amount of jitter on the x-axis.
Matplotlib
Need a hint?

Just assign the number 0.1 to the variable jitter_strength.

3
Create jittered x positions and plot scatter points
Import matplotlib.pyplot as plt and random. Use a for loop with variables cookie and sales to iterate over cookie_sales.items(). Inside the loop, create a list x_positions by adding random jitter between -jitter_strength and jitter_strength to the cookie's index. Then plot the scatter points with plt.scatter(x_positions, sales). Use enumerate(cookie_sales) to get the index of each cookie type.
Matplotlib
Need a hint?

Use enumerate(cookie_sales) to get the index and cookie name. Use a list comprehension to add random jitter to the index for each sale.

4
Show the scatter plot
Add the line plt.show() to display the scatter plot with jittered points.
Matplotlib
Need a hint?

Use plt.show() to display the plot window.