📖 Scenario: You are working with sales data for different fruits in a small grocery store. You want to show the sales distribution using a pie chart. To make the chart clearer and more attractive, you want to customize the colors of each slice.
🎯 Goal: Create a pie chart that shows the sales of fruits with custom colors for each fruit slice.
📋 What You'll Learn
Create a dictionary called fruit_sales with exact fruit names and sales numbers.
Create a list called colors with exact color names matching the fruits order.
Use matplotlib's plt.pie() function with the colors parameter to customize slice colors.
Print the pie chart with a title 'Fruit Sales Distribution'.
💡 Why This Matters
🌍 Real World
Pie charts are often used in business to show parts of a whole, like sales distribution among products.
💼 Career
Data analysts and scientists use pie charts to communicate data insights clearly to stakeholders.
Progress0 / 4 steps
1
Create the fruit sales data
Create a dictionary called fruit_sales with these exact entries: 'Apples': 30, 'Bananas': 15, 'Cherries': 45, 'Dates': 10.
Matplotlib
Hint
Use curly braces {} to create a dictionary with keys as fruit names and values as sales numbers.
2
Create the colors list
Create a list called colors with these exact color names in order: 'red', 'yellow', 'pink', 'brown'.
Matplotlib
Hint
Use square brackets [] to create a list of color names as strings.
3
Create the pie chart with custom colors
Import matplotlib.pyplot as plt. Use plt.pie() with fruit_sales.values() as data and colors=colors to set slice colors. Add labels using fruit_sales.keys(). Add a title 'Fruit Sales Distribution' using plt.title().
Matplotlib
Hint
Remember to import matplotlib.pyplot as plt first. Use the colors parameter inside plt.pie() to customize colors.
4
Display the pie chart
Use plt.show() to display the pie chart.
Matplotlib
Hint
Use plt.show() to make the pie chart appear on the screen.
Practice
(1/5)
1. What does the colors parameter do in a matplotlib pie chart?
easy
A. It changes the size of the pie chart.
B. It changes the chart title.
C. It adds labels to the pie slices.
D. It sets the colors of each slice in the pie chart.
Solution
Step 1: Understand the role of colors in pie charts
The colors parameter is used to specify the colors for each slice of the pie chart.
Step 2: Compare with other options
Options A, C, and D relate to size, labels, and title, which are controlled by other parameters.
Final Answer:
It sets the colors of each slice in the pie chart. -> Option D
The colors parameter must be a list of color names or hex codes, but here it is a single string.
Step 2: Understand the impact of wrong type
Passing a string will cause matplotlib to treat it as one color or raise an error.
Final Answer:
colors should be a list, not a string -> Option A
Quick Check:
colors must be list, not string [OK]
Hint: Always pass colors as a list, not a single string [OK]
Common Mistakes:
Passing colors as comma-separated string
Ignoring type requirements for colors
Assuming plt.show() is optional
5. You want to create a pie chart with 4 slices and use custom colors: red, green, blue, and yellow. Which code correctly applies these colors and also adds labels for each slice?