📖 Scenario: Imagine you run a small bakery. You want to see which types of cakes sell the most each day. You have sales numbers for three cake types: Chocolate, Vanilla, and Strawberry.
🎯 Goal: You will create a bar chart to compare the sales of these cake types. This will help you see which cake is the most popular at a glance.
📋 What You'll Learn
Create a dictionary called cake_sales with exact entries: 'Chocolate': 30, 'Vanilla': 45, 'Strawberry': 25
Create a list called categories that contains the keys from cake_sales
Create a list called sales that contains the values from cake_sales
Use matplotlib.pyplot to create a bar chart with categories on the x-axis and sales on the y-axis
Add a title to the chart: 'Cake Sales Comparison'
Label the x-axis as 'Cake Type' and the y-axis as 'Number Sold'
Display the bar chart
💡 Why This Matters
🌍 Real World
Bar charts help businesses quickly compare sales or counts across different categories, like product types or customer groups.
💼 Career
Data analysts and scientists use bar charts to communicate insights clearly to teams and decision makers.
Progress0 / 4 steps
1
Create the cake sales data
Create a dictionary called cake_sales with these exact entries: 'Chocolate': 30, 'Vanilla': 45, 'Strawberry': 25
Matplotlib
Hint
Use curly braces {} to create a dictionary with keys and values.
2
Prepare categories and sales lists
Create a list called categories that contains the keys from cake_sales. Then create a list called sales that contains the values from cake_sales.
Matplotlib
Hint
Use list() with cake_sales.keys() and cake_sales.values() to get lists.
3
Create the bar chart
Import matplotlib.pyplot as plt. Use plt.bar() with categories and sales to create a bar chart. Add the title 'Cake Sales Comparison'. Label the x-axis as 'Cake Type' and the y-axis as 'Number Sold'.
Matplotlib
Hint
Use plt.bar() to create bars, and plt.title(), plt.xlabel(), plt.ylabel() to add text.
4
Show the bar chart
Use plt.show() to display the bar chart.
Matplotlib
Hint
Use plt.show() to open the chart window.
Practice
(1/5)
1. Why do bar charts use bars of different heights to represent data?
easy
A. To show data in a circular format
B. To display data as a continuous line
C. To show differences between categories clearly
D. To represent data using colors only
Solution
Step 1: Understand bar chart basics
Bar charts use bars where height or length shows the value for each category.
Step 2: Identify purpose of different bar heights
Different heights make it easy to compare values visually across categories.
Final Answer:
To show differences between categories clearly -> Option C
Quick Check:
Bar height = value difference [OK]
Hint: Bar height shows value size, making comparison easy [OK]
Common Mistakes:
Thinking bar charts show continuous trends like line charts
Confusing bar height with color meaning
Assuming bar charts are for circular data
2. Which of the following is the correct way to create a simple bar chart in matplotlib?
easy
A. plt.plot(['A', 'B'], [5, 7])
B. plt.bar(['A', 'B'], [5, 7])
C. plt.scatter(['A', 'B'], [5, 7])
D. plt.hist(['A', 'B'], [5, 7])
Solution
Step 1: Recall matplotlib bar chart syntax
The function plt.bar() creates bar charts with categories and their values.
Step 2: Compare options to syntax
Only plt.bar(['A', 'B'], [5, 7]) matches the correct bar chart syntax.
Final Answer:
plt.bar(['A', 'B'], [5, 7]) -> Option B
Quick Check:
plt.bar() = bar chart [OK]
Hint: Use plt.bar() for bar charts, not plt.plot() or plt.scatter() [OK]
D. The sales list has fewer values than the products list
Solution
Step 1: Check lengths of inputs
Products has 3 items, sales has 2 items; lengths must match for plt.bar().
Step 2: Understand error cause
Mismatch causes ValueError because matplotlib can't pair categories with missing values.
Final Answer:
The sales list has fewer values than the products list -> Option D
Quick Check:
Equal lengths needed for categories and values [OK]
Hint: Ensure categories and values lists have same length [OK]
Common Mistakes:
Thinking plt.bar() can't use string categories
Forgetting plt.show() causes no plot but no error
Assuming numeric categories are required
5. You have sales data for four stores: Store A (100), Store B (150), Store C (90), Store D (150). You want to create a bar chart comparing these stores but want to highlight stores with the highest sales in a different color. How can you do this in matplotlib?
hard
A. Use plt.bar() with a color list that sets a different color for stores with sales 150
B. Use plt.plot() to draw lines connecting sales values
C. Use plt.bar() but set all bars to the same color
D. Use plt.scatter() with sales as x and stores as y
Solution
Step 1: Identify highest sales values
Stores B and D have the highest sales (150).
Step 2: Create color list for bars
Assign a special color (e.g., red) to bars for sales 150, and default color for others.
Step 3: Use plt.bar() with colors
Pass the color list to plt.bar() to highlight highest sales bars.
Final Answer:
Use plt.bar() with a color list that sets a different color for stores with sales 150 -> Option A
Quick Check:
Color list highlights categories by value [OK]
Hint: Use color list in plt.bar() to highlight specific bars [OK]
Common Mistakes:
Using plt.plot() which makes line charts, not bars
Setting all bars same color loses highlight
Using plt.scatter() which is for points, not bars
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.