0
0
Matplotlibdata~30 mins

Why pie charts show proportions in Matplotlib - See It in Action

Choose your learning style9 modes available
Why Pie Charts Show Proportions
📖 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
Need a 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
Need a 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
Need a 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
Need a 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.