📖 Scenario: Imagine you run a small bakery and want to understand which types of pastries sell the most. You have sales data for different pastry types and want to visualize their share of total sales.
🎯 Goal: You will create a pie chart using matplotlib to show the proportions of pastry sales. This will help you see which pastries are most popular at a glance.
📋 What You'll Learn
Create a dictionary called pastry_sales with exact pastry names and sales numbers
Create a variable called total_sales that sums all sales
Use a for loop with variables pastry and sales to calculate proportions
Use matplotlib.pyplot.pie() to create a pie chart showing proportions
Print the proportions dictionary to see the share of each pastry
💡 Why This Matters
🌍 Real World
Pie charts are used in business to quickly show how parts make up a whole, like sales shares or budget spending.
💼 Career
Data analysts and scientists use pie charts to communicate proportions clearly to stakeholders and help make decisions.
Progress0 / 4 steps
1
Create the pastry sales data
Create a dictionary called pastry_sales with these exact entries: 'Croissant': 120, 'Muffin': 80, 'Danish': 50, 'Eclair': 30
Matplotlib
Hint
Use curly braces {} to create a dictionary with pastry names as keys and sales numbers as values.
2
Calculate total sales
Create a variable called total_sales that sums all the sales values in pastry_sales using the sum() function and .values() method
Matplotlib
Hint
Use sum(pastry_sales.values()) to add all sales numbers.
3
Calculate proportions of each pastry
Create an empty dictionary called proportions. Use a for loop with variables pastry and sales to iterate over pastry_sales.items(). Inside the loop, calculate the proportion as sales / total_sales and store it in proportions[pastry]
Matplotlib
Hint
Use a for loop to go through each pastry and calculate its share by dividing sales by total_sales.
4
Create and show the pie chart
Import matplotlib.pyplot as plt. Use plt.pie() with the sales values from pastry_sales.values() and labels from pastry_sales.keys(). Add autopct='%1.1f%%' to show percentages. Use plt.title() to add the title 'Pastry Sales Proportions'. Finally, call plt.show() to display the chart. Also, print the proportions dictionary.
Matplotlib
Hint
Remember to import matplotlib.pyplot as plt. Use plt.pie() with sales values and labels, add autopct for percentages, then show the plot with plt.show(). Finally, print the proportions dictionary.
Practice
(1/5)
1. Why do pie charts show slices of different sizes?
easy
A. Because each slice represents a proportion of the total
B. Because slices are randomly sized
C. Because all slices must be equal
D. Because slices represent unrelated data points
Solution
Step 1: Understand what pie chart slices represent
Each slice shows a part of the whole data set.
Step 2: Connect slice size to proportion
The size of each slice matches how big that part is compared to the total.
Final Answer:
Because each slice represents a proportion of the total -> Option A
Quick Check:
Slice size = proportion [OK]
Hint: Remember: pie slices show parts of a whole [OK]
Common Mistakes:
Thinking slices are random sizes
Assuming all slices are equal
Believing slices show unrelated data
2. Which matplotlib function is used to create a pie chart?
easy
A. plt.bar()
B. plt.pie()
C. plt.scatter()
D. plt.plot()
Solution
Step 1: Recall matplotlib plotting functions
Matplotlib has specific functions for different chart types.
Step 2: Identify the pie chart function
The function plt.pie() is designed to create pie charts.
C. The labels list length does not match sizes list length
D. plt.show() is missing
Solution
Step 1: Check sizes and labels lengths
Sizes has 3 values, labels has only 2 labels.
Step 2: Understand matplotlib requirements
Labels must match the number of slices to label each slice properly.
Final Answer:
The labels list length does not match sizes list length -> Option C
Quick Check:
Labels count = sizes count [OK]
Hint: Labels count must equal sizes count [OK]
Common Mistakes:
Thinking sizes values are invalid
Believing plt.pie() limits slices
Forgetting plt.show() is needed
5. You want to show proportions of sales for 4 products using a pie chart. The sales are [100, 200, 300, 400]. How do you ensure the pie chart correctly shows proportions?
hard
A. Pass the sales list directly to plt.pie() and add labels for each product
B. Normalize sales to percentages before passing to plt.pie()
C. Use plt.bar() instead because pie charts need percentages
D. Divide each sales value by 1000 before plotting
Solution
Step 1: Understand how plt.pie() works with data
Matplotlib automatically calculates proportions from raw values.
Step 2: Confirm labels and data match
Passing raw sales with matching labels shows correct slice sizes.
Final Answer:
Pass the sales list directly to plt.pie() and add labels for each product -> Option A
Quick Check:
Raw values work; no manual normalization needed [OK]
Hint: Pass raw values; matplotlib calculates proportions [OK]
Common Mistakes:
Thinking manual percentage calculation is needed
Using bar chart instead of pie chart
Scaling values unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.