0
0
Matplotlibdata~30 mins

Percentage labels in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Percentage labels
📖 Scenario: You work in a small bakery and want to show how much each type of pastry contributes to total sales. You want to create a pie chart that shows the percentage share of each pastry type.
🎯 Goal: Create a pie chart using matplotlib that displays the percentage labels for each pastry type's sales.
📋 What You'll Learn
Create a dictionary called pastry_sales with exact entries for pastry types and their sales numbers.
Create a variable called labels that holds the pastry names from the dictionary.
Create a variable called sizes that holds the sales numbers from the dictionary.
Use matplotlib.pyplot.pie to create a pie chart with percentage labels shown.
Print the pie chart to display it.
💡 Why This Matters
🌍 Real World
Pie charts with percentage labels are useful to show parts of a whole in sales reports, surveys, and business presentations.
💼 Career
Data analysts and business intelligence professionals often create pie charts to communicate data insights clearly to stakeholders.
Progress0 / 4 steps
1
Create the pastry sales data
Create a dictionary called pastry_sales with these exact entries: 'Croissant': 120, 'Muffin': 90, 'Danish': 60, 'Eclair': 30.
Matplotlib
Need a hint?

Use curly braces {} to create the dictionary with the exact pastry names and sales numbers.

2
Extract labels and sizes
Create a list called labels that contains the keys from pastry_sales. Create a list called sizes that contains the values from pastry_sales.
Matplotlib
Need a hint?

Use list() with pastry_sales.keys() and pastry_sales.values() to get the lists.

3
Create the pie chart with percentage labels
Import matplotlib.pyplot as plt. Use plt.pie with sizes and labels. Add the argument autopct='%1.1f%%' to show percentage labels on the pie slices.
Matplotlib
Need a hint?

Use import matplotlib.pyplot as plt and then plt.pie() with the correct arguments.

4
Display the pie chart
Use plt.show() to display the pie chart.
Matplotlib
Need a hint?

Call plt.show() to open the window with the pie chart.