Pie charts show parts of a whole. Changing colors helps make each part clear and pretty.
Pie chart color customization in Matplotlib
Introduction
Syntax
Matplotlib
plt.pie(data, labels=labels, colors=colors)
colors is a list of color names or codes matching each slice.
Colors can be named colors like 'red', hex codes like '#FF0000', or RGB tuples.
Examples
Matplotlib
plt.pie([10, 20, 30], labels=['A', 'B', 'C'], colors=['red', 'green', 'blue'])
Matplotlib
plt.pie([5, 15, 25], labels=['X', 'Y', 'Z'], colors=['#FF6347', '#4682B4', '#32CD32'])
Matplotlib
plt.pie([7, 14, 21], labels=['One', 'Two', 'Three'], colors=[(1,0,0), (0,1,0), (0,0,1)])
Sample Program
This program creates a pie chart showing fruit distribution. Each slice has a soft color for easy reading. Percentages are shown on slices.
Matplotlib
import matplotlib.pyplot as plt # Data for pie chart sizes = [40, 35, 25] labels = ['Apples', 'Bananas', 'Cherries'] colors = ['#FF9999', '#FFE699', '#99FF99'] # soft red, yellow, green plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%') plt.title('Fruit Distribution') plt.show()
Important Notes
If you provide fewer colors than slices, matplotlib repeats colors.
Use autopct to add percentage labels on slices.
Colors can be customized to improve accessibility and style.
Summary
Pie chart colors make parts easy to see and understand.
Use the colors parameter with a list of colors.
Colors can be names, hex codes, or RGB tuples.
Practice
1. What does the
colors parameter do in a matplotlib pie chart?easy
Solution
Step 1: Understand the role of
Thecolorsin pie chartscolorsparameter 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 DQuick Check:
colors parameter = slice colors [OK]
Hint: Remember: colors parameter controls slice colors [OK]
Common Mistakes:
- Confusing colors with labels
- Thinking colors change chart size
- Mixing colors with title settings
2. Which of the following is the correct way to set colors in a matplotlib pie chart?
easy
Solution
Step 1: Recall the correct parameter name
The correct parameter to set slice colors iscolors, notcolor,col, orset_colors.Step 2: Check syntax correctness
plt.pie(data, colors=['red', 'blue', 'green']) usescolorswith a list of color names, which is the correct syntax.Final Answer:
plt.pie(data, colors=['red', 'blue', 'green']) -> Option AQuick Check:
Use colors= list for slice colors [OK]
Hint: Use exact parameter name 'colors' for slice colors [OK]
Common Mistakes:
- Using 'color' instead of 'colors'
- Using undefined parameters like 'col' or 'set_colors'
- Passing colors as a string instead of a list
3. What will be the color of the second slice in this code?
import matplotlib.pyplot as plt sizes = [30, 40, 30] colors = ['#FF0000', '#00FF00', '#0000FF'] plt.pie(sizes, colors=colors) plt.show()
medium
Solution
Step 1: Identify the colors list order
The colors list is ['#FF0000', '#00FF00', '#0000FF'], which correspond to red, green, and blue in order.Step 2: Match the second slice color
The second slice uses the second color '#00FF00', which is green.Final Answer:
Green -> Option CQuick Check:
Second color in list = green [OK]
Hint: Colors list order matches slice order [OK]
Common Mistakes:
- Mixing hex codes with color names
- Assuming default colors override given list
- Confusing slice index starting at 0 or 1
4. Find the error in this pie chart color code:
import matplotlib.pyplot as plt sizes = [25, 25, 50] colors = 'red, blue, green' plt.pie(sizes, colors=colors) plt.show()
medium
Solution
Step 1: Check the type of colors parameter
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 AQuick 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?
hard
Solution
Step 1: Verify the colors parameter and list length
The colors parameter must be named exactly 'colors' and have 4 colors for 4 slices.Step 2: Check the labels parameter name and length
The labels parameter must be named 'labels' and have 4 labels matching the slices.Step 3: Identify errors in other options
plt.pie([10, 20, 30, 40], color=['red', 'green', 'blue', 'yellow'], labels=['A', 'B', 'C', 'D']) uses 'color' instead of 'colors'. plt.pie([10, 20, 30, 40], colors=['red', 'green', 'blue'], labels=['A', 'B', 'C', 'D']) has only 3 colors for 4 slices. plt.pie([10, 20, 30, 40], colors=['red', 'green', 'blue', 'yellow'], label=['A', 'B', 'C', 'D']) uses 'label' instead of 'labels'.Final Answer:
plt.pie([10, 20, 30, 40], colors=['red', 'green', 'blue', 'yellow'], labels=['A', 'B', 'C', 'D']) -> Option BQuick Check:
Use colors= and labels= with matching list lengths [OK]
Hint: Match colors and labels list lengths with data slices [OK]
Common Mistakes:
- Using wrong parameter names like 'color' or 'label'
- Mismatched list lengths for colors or labels
- Forgetting to pass labels parameter
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
