What is the purpose of percentage labels in a matplotlib pie chart?
Percentage labels show the part each slice represents of the whole, making it easier to understand proportions visually.
Click to reveal answer
beginner
How do you add percentage labels to a pie chart in matplotlib?
Use the autopct parameter in plt.pie() and set it to a format string like '%1.1f%%' to display percentages with one decimal place.
Click to reveal answer
beginner
What does the format string '%1.1f%%' mean in the context of percentage labels?
It means show a floating-point number with at least one digit before and one digit after the decimal point, followed by a percent sign.
Click to reveal answer
intermediate
Can you customize the appearance of percentage labels in matplotlib pie charts?
Yes, you can customize font size, color, and style by using additional parameters like textprops in plt.pie().
Click to reveal answer
beginner
Why might you want to use percentage labels instead of raw values in a pie chart?
Percentage labels help viewers quickly understand the relative size of each slice compared to the whole, which is often more meaningful than raw numbers.
Click to reveal answer
Which parameter in plt.pie() adds percentage labels to slices?
Acolors
Blabels
Cautopct
Dexplode
✗ Incorrect
The autopct parameter formats and displays percentage labels on pie chart slices.
What does the format string '%1.0f%%' display in percentage labels?
APercentage with no decimal places
BPercentage with one decimal place
CRaw values without percentage sign
DPercentage with two decimal places
✗ Incorrect
The '%1.0f%%' format shows percentages rounded to whole numbers with no decimals.
How can you change the font size of percentage labels in matplotlib pie charts?
AUse the <code>textprops</code> parameter with a dictionary including <code>{'fontsize': value}</code>
BChange the <code>autopct</code> string
CSet the <code>labels</code> parameter
DUse the <code>colors</code> parameter
✗ Incorrect
The textprops parameter lets you customize text properties like font size.
If you want to show percentages with one decimal place, which autopct value should you use?
A'%1.0f%%'
B'%1.1f%%'
C'%2.1f%%'
D'%0.1f%%'
✗ Incorrect
The '%1.1f%%' format shows one digit before and one digit after the decimal point.
What is the main benefit of adding percentage labels to pie charts?
AThey change the colors of slices
BThey explode slices
CThey add legends automatically
DThey help viewers understand the relative size of each slice
✗ Incorrect
Percentage labels make it easier to see how big each slice is compared to the whole.
Explain how to add and customize percentage labels in a matplotlib pie chart.
Think about the parameters in plt.pie() that control labels and text style.
You got /3 concepts.
Why are percentage labels useful in pie charts compared to raw values?
Consider what helps people quickly grasp data proportions.
You got /3 concepts.
Practice
(1/5)
1. What does the autopct parameter do in plt.pie() when creating a pie chart?
easy
A. It adds percentage labels to each slice of the pie chart.
B. It changes the colors of the pie slices.
C. It sets the title of the pie chart.
D. It adjusts the size of the pie chart.
Solution
Step 1: Understand the role of autopct in plt.pie()
The autopct parameter is used to display the percentage value on each slice of the pie chart.
Step 2: Compare with other parameters
Other parameters like colors or title do not control percentage labels; only autopct does.
Final Answer:
It adds percentage labels to each slice of the pie chart. -> Option A
Quick Check:
autopct = percentage labels [OK]
Hint: Remember autopct shows percentages on pie slices [OK]
Common Mistakes:
Confusing autopct with color or title parameters
Thinking autopct changes chart size
Ignoring autopct and missing percentage labels
2. Which of the following is the correct syntax to show percentages with one decimal place in a pie chart using plt.pie()?
easy
A. plt.pie(data, autopct='%.1f%')
B. plt.pie(data, autopct='%f1.1%')
C. plt.pie(data, autopct='%1.1f%%')
D. plt.pie(data, autopct='%1.f%%')
Solution
Step 1: Recall the format string for percentages
The correct format for one decimal place is '%1.1f%%' where '%1.1f' formats the float and '%%' prints the percent sign.
Step 2: Check each option's format
plt.pie(data, autopct='%1.1f%%') matches the correct format. Options A, B, and D have misplaced or missing percent signs or incorrect format specifiers.
Final Answer:
plt.pie(data, autopct='%1.1f%%') -> Option C
Quick Check:
Format string '%1.1f%%' = correct percentage format [OK]
Hint: Use '%1.1f%%' to show one decimal percent [OK]
Common Mistakes:
Forgetting to double the percent sign '%%'
Misplacing the decimal point in format
Using incorrect format specifiers like '%f1.1%'
3. What will be the output percentages shown on the pie chart slices for the following code?
A. The format string '%1.1f%' is missing an extra percent sign.
B. The sizes list should be a numpy array.
C. plt.show() is missing parentheses.
D. autopct should be set to True instead of a format string.
Solution
Step 1: Check the autopct format string
The format string '%1.1f%' is incorrect because a single '%' is interpreted as a format specifier start, so to print a percent sign, '%%' is needed.
Step 2: Verify other parts of the code
The sizes list can be a list, plt.show() has parentheses, and autopct expects a format string, not True.
Final Answer:
The format string '%1.1f%' is missing an extra percent sign. -> Option A
Quick Check:
Use '%%' to print percent sign in autopct [OK]
Hint: Double percent signs '%%' to show % in autopct [OK]
Common Mistakes:
Using single '%' instead of '%%' in format string
Thinking autopct accepts boolean True
Assuming list must be numpy array
5. You want to create a pie chart with slices labeled by percentage, but only show percentages for slices larger than 20%. Which autopct function below correctly implements this?
hard
A. lambda p: f'{p:.1f}%'
B. lambda p: f'{p:.1f}%' if p < 20 else ''
C. lambda p: f'{p:.0f}%' if p >= 20 else '0%'
D. lambda p: f'{p:.1f}%' if p > 20 else ''
Solution
Step 1: Understand the requirement
Show percentage labels only if slice percentage is greater than 20%, else show nothing.
Step 2: Analyze each lambda function
lambda p: f'{p:.1f}%' if p > 20 else '' returns formatted percentage if p > 20, else empty string. lambda p: f'{p:.1f}%' if p < 20 else '' does the opposite. lambda p: f'{p:.0f}%' if p >= 20 else '0%' shows '0%' for smaller slices, which is not desired. lambda p: f'{p:.1f}%' shows all percentages.
Final Answer:
lambda p: f'{p:.1f}%' if p > 20 else '' -> Option D
Quick Check:
Show label only if p > 20% = lambda p: f'{p:.1f}%' if p > 20 else '' [OK]
Hint: Use lambda with condition to filter percentages [OK]
Common Mistakes:
Showing percentages for all slices
Using wrong comparison operator (< instead of >)
Returning '0%' instead of empty string for small slices
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.