📖 Scenario: Imagine you work in a small bakery. You want to understand which types of bread sell the most each day. You have data about bread types and how many were sold. Visualizing this data helps you see patterns quickly.
🎯 Goal: You will create a simple bar chart using matplotlib to visualize sales of different bread types. This will help you understand why categorical visualization is important for comparing groups.
📋 What You'll Learn
Create a dictionary with bread types and their sales
Set a color for the bars
Use a bar chart to show sales by bread type
Display the chart with labels
💡 Why This Matters
🌍 Real World
Businesses often need to compare categories like product sales, customer types, or survey answers. Visual charts make these comparisons clear and fast.
💼 Career
Data analysts and scientists use categorical visualizations to communicate insights and support decisions in marketing, sales, and operations.
Progress0 / 4 steps
1
Create the sales data dictionary
Create a dictionary called bread_sales with these exact entries: 'Sourdough': 30, 'Baguette': 45, 'Rye': 20, 'Whole Wheat': 35.
Matplotlib
Hint
Use curly braces {} to create the dictionary with keys as bread types and values as sales numbers.
2
Set the bar color
Create a variable called bar_color and set it to the string 'skyblue' to use as the color for the bars in the chart.
Matplotlib
Hint
Assign the string 'skyblue' to the variable bar_color.
3
Create the bar chart
Use matplotlib.pyplot to create a bar chart. Use plt.bar() with the keys of bread_sales as categories and the values as heights. Use the bar_color variable for the color. Import matplotlib.pyplot as plt.
Matplotlib
Hint
Use plt.bar() with bread_sales.keys() and bread_sales.values(). Pass color=bar_color.
4
Show the chart with labels
Add labels for the x-axis as 'Bread Type' and y-axis as 'Number Sold' using plt.xlabel() and plt.ylabel(). Then use plt.show() to display the chart.
Matplotlib
Hint
Use plt.xlabel('Bread Type') and plt.ylabel('Number Sold'). Then call plt.show() to display the chart.
Practice
(1/5)
1. Why is categorical visualization important in data science?
easy
A. It shows exact numerical calculations only.
B. It helps compare data grouped by categories clearly.
C. It replaces the need for any data cleaning.
D. It automatically predicts future data trends.
Solution
Step 1: Understand the purpose of categorical visualization
Categorical visualization groups data by categories to make comparisons easy and clear.
Step 2: Evaluate the options based on this purpose
Only It helps compare data grouped by categories clearly. correctly states that it helps compare grouped data clearly; others describe unrelated functions.
Final Answer:
It helps compare data grouped by categories clearly. -> Option B
B. The values list should be strings, not numbers.
C. plt.show() is missing.
D. The categories and values lists have different lengths.
Solution
Step 1: Check lengths of categories and values
Categories list has 3 items, values list has 2 items, causing mismatch error.
Step 2: Verify other options
plt.bar() accepts string categories, plt.show() is present, and values should be numbers for bar heights.
Final Answer:
The categories and values lists have different lengths. -> Option D
Quick Check:
List length mismatch = error [OK]
Hint: Check if categories and values lists match in length [OK]
Common Mistakes:
Assuming plt.bar() can't use strings
Forgetting plt.show()
Using strings for values instead of numbers
5. You have sales data for three products: 'Shoes', 'Hats', and 'Shirts' with sales 120, 80, and 150 respectively. You want to create a bar chart that clearly shows which product sold the most. Which steps should you follow to make the chart easy to understand?
hard
A. Use plt.scatter() with product names as x and sales as y, no title.
B. Use plt.plot() with sales only, no labels needed.
C. Use plt.bar() with product names and sales, add clear labels and a title.
D. Print sales numbers only, no chart needed.
Solution
Step 1: Choose the correct plot type for categorical data
Bar charts (plt.bar()) are best to compare sales by product category clearly.
Step 2: Add labels and title for clarity
Adding x-axis labels (product names), y-axis labels (sales), and a title helps others understand the chart quickly.
Final Answer:
Use plt.bar() with product names and sales, add clear labels and a title. -> Option C
Quick Check:
Bar chart + labels + title = clear visualization [OK]
Hint: Bar chart + labels + title = clear category comparison [OK]
Common Mistakes:
Using line or scatter plots for categories
Skipping labels and titles
Not visualizing data at all
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.