📖 Scenario: You work at a small bakery. You want to show how many cakes of each type you sold last week. A bar chart is a great way to show this information clearly.
🎯 Goal: Create a bar chart that shows the number of cakes sold for each cake type.
📋 What You'll Learn
Create a dictionary with cake types and their sales numbers
Create a list of cake types from the dictionary keys
Create a list of sales numbers from the dictionary values
Use matplotlib to create a bar chart with cake types on the x-axis and sales numbers on the y-axis
Add labels for the x-axis, y-axis, and a title to the chart
Display the bar chart
💡 Why This Matters
🌍 Real World
Bar charts are used in business to show sales, in schools to show test scores, and in many places to compare amounts clearly.
💼 Career
Data analysts and scientists use bar charts to communicate data insights visually to teams and decision makers.
Progress0 / 4 steps
1
Create the sales data dictionary
Create a dictionary called cake_sales with these exact entries: 'Chocolate': 30, 'Vanilla': 45, 'Strawberry': 25, 'Lemon': 20.
Data Analysis Python
Hint
Use curly braces {} to create a dictionary. Separate keys and values with a colon : and items with commas.
2
Prepare lists for the bar chart
Create a list called cake_types containing the keys of cake_sales. Create another list called sales_numbers containing the values of cake_sales.
Data Analysis Python
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 cake_types and sales_numbers to create the bar chart. Add x-axis label 'Cake Types', y-axis label 'Number Sold', and title 'Cake Sales Last Week' using plt.xlabel(), plt.ylabel(), and plt.title().
Data Analysis Python
Hint
Use plt.bar(x, y) to create bars. Use plt.xlabel(), plt.ylabel(), and plt.title() to add labels and title.
4
Display the bar chart
Use plt.show() to display the bar chart.
Data Analysis Python
Hint
Call plt.show() to open a window with the bar chart.
Practice
(1/5)
1. What does a bar chart mainly help you do with data?
easy
A. See differences between groups using bars
B. Calculate exact averages of numbers
C. Show data as a continuous line
D. Display data in a table format
Solution
Step 1: Understand the purpose of bar charts
Bar charts use bars to visually compare values across categories.
Step 2: Compare options to bar chart use
Only See differences between groups using bars describes using bars to see differences between groups.
Final Answer:
See differences between groups using bars -> Option A
Quick Check:
Bar charts = Visual group comparison [OK]
Hint: Bar charts show group sizes as bars [OK]
Common Mistakes:
Confusing bar charts with line charts
Thinking bar charts show exact calculations
Mixing bar charts with tables
2. Which of these is the correct way to create a vertical bar chart using matplotlib in Python?
Using plt.bar() causes vertical bars, so to get horizontal bars, plt.barh() is needed.
Final Answer:
plt.bar() creates vertical bars; use plt.barh() for horizontal bars -> Option D
Quick Check:
Horizontal bars need plt.barh() [OK]
Hint: Use plt.barh() for horizontal bars [OK]
Common Mistakes:
Thinking labels must be numbers
Swapping order of plt.show() and plt.bar()
Using string values instead of numbers
5. You have sales data for 3 products: {'Product A': 30, 'Product B': 45, 'Product C': 25}. You want to create a bar chart showing these sales with product names on the x-axis and sales on the y-axis. Which code snippet correctly creates this chart with labels and a title?
hard
A. import matplotlib.pyplot as plt
sales = {'Product A': 30, 'Product B': 45, 'Product C': 25}
plt.barh(sales.values(), sales.keys())
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales by Product')
plt.show()
B. import matplotlib.pyplot as plt
sales = {'Product A': 30, 'Product B': 45, 'Product C': 25}
plt.bar(sales.keys(), sales.values())
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales by Product')
plt.show()
C. import matplotlib.pyplot as plt
sales = {'Product A': 30, 'Product B': 45, 'Product C': 25}
plt.plot(sales.keys(), sales.values())
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales by Product')
plt.show()
D. import matplotlib.pyplot as plt
sales = {'Product A': 30, 'Product B': 45, 'Product C': 25}
plt.bar(sales.values(), sales.keys())
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales by Product')
plt.show()
Solution
Step 1: Understand data and desired chart
We want vertical bars with product names on x-axis and sales on y-axis.
Step 2: Check code snippets for correct bar chart
import matplotlib.pyplot as plt
sales = {'Product A': 30, 'Product B': 45, 'Product C': 25}
plt.bar(sales.keys(), sales.values())
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales by Product')
plt.show() uses plt.bar() with keys as x and values as heights, plus labels and title correctly set.
Step 3: Identify errors in other options
import matplotlib.pyplot as plt
sales = {'Product A': 30, 'Product B': 45, 'Product C': 25}
plt.barh(sales.values(), sales.keys())
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales by Product')
plt.show() uses plt.barh() but swaps x and y labels incorrectly. import matplotlib.pyplot as plt
sales = {'Product A': 30, 'Product B': 45, 'Product C': 25}
plt.plot(sales.keys(), sales.values())
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales by Product')
plt.show() uses plt.plot() which is a line chart. import matplotlib.pyplot as plt
sales = {'Product A': 30, 'Product B': 45, 'Product C': 25}
plt.bar(sales.values(), sales.keys())
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales by Product')
plt.show() swaps keys and values in plt.bar(), causing wrong axes.
Final Answer:
Code that correctly creates the labeled vertical bar chart -> Option B
Quick Check:
Keys on x, values on y with plt.bar() [OK]
Hint: Use plt.bar(keys, values) with labels and title [OK]
Common Mistakes:
Swapping keys and values in plt.bar()
Using plt.plot() instead of plt.bar()
Mixing up plt.bar() and plt.barh() axes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.