Percentage labels in Matplotlib - Time & Space Complexity
We want to understand how the time needed to add percentage labels in a matplotlib chart changes as the data size grows.
How does the work increase when we add more slices to a pie chart with percentage labels?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
sizes = [10, 20, 30, 40]
labels = ['A', 'B', 'C', 'D']
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.show()
This code creates a pie chart with four slices and shows percentage labels on each slice.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Drawing each slice and calculating its percentage label.
- How many times: Once for each slice in the data list.
As the number of slices increases, the work to draw and label each slice grows directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 slice drawings and 10 percentage calculations |
| 100 | 100 slice drawings and 100 percentage calculations |
| 1000 | 1000 slice drawings and 1000 percentage calculations |
Pattern observation: The work grows in a straight line as the number of slices increases.
Time Complexity: O(n)
This means the time to add percentage labels grows directly with the number of slices.
[X] Wrong: "Adding percentage labels takes the same time no matter how many slices there are."
[OK] Correct: Each slice needs its own label calculated and drawn, so more slices mean more work.
Understanding how labeling scales helps you explain performance when visualizing larger datasets clearly and confidently.
"What if we added custom labels with complex formatting instead of simple percentages? How would the time complexity change?"
Practice
autopct parameter do in plt.pie() when creating a pie chart?Solution
Step 1: Understand the role of autopct in plt.pie()
Theautopctparameter 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; onlyautopctdoes.Final Answer:
It adds percentage labels to each slice of the pie chart. -> Option AQuick Check:
autopct = percentage labels [OK]
- Confusing autopct with color or title parameters
- Thinking autopct changes chart size
- Ignoring autopct and missing percentage labels
plt.pie()?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 CQuick Check:
Format string '%1.1f%%' = correct percentage format [OK]
- Forgetting to double the percent sign '%%'
- Misplacing the decimal point in format
- Using incorrect format specifiers like '%f1.1%'
import matplotlib.pyplot as plt sizes = [10, 20, 30] plt.pie(sizes, autopct='%1.0f%%') plt.show()
Solution
Step 1: Calculate total and percentages
Total = 10 + 20 + 30 = 60. Percentages: 10/60=16.67%, 20/60=33.33%, 30/60=50%.Step 2: Apply autopct format '%1.0f%%'
This rounds percentages to nearest whole number: 17%, 33%, 50%.Final Answer:
17%, 33%, 50% -> Option BQuick Check:
Rounded percentages = 17%, 33%, 50% [OK]
- Using raw values instead of percentages
- Not rounding percentages correctly
- Assuming equal slices without calculation
import matplotlib.pyplot as plt sizes = [40, 60] plt.pie(sizes, autopct='%1.1f%') plt.show()
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 AQuick Check:
Use '%%' to print percent sign in autopct [OK]
- Using single '%' instead of '%%' in format string
- Thinking autopct accepts boolean True
- Assuming list must be numpy array
autopct function below correctly implements this?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 DQuick Check:
Show label only if p > 20% = lambda p: f'{p:.1f}%' if p > 20 else '' [OK]
- Showing percentages for all slices
- Using wrong comparison operator (< instead of >)
- Returning '0%' instead of empty string for small slices
Ready to prove it?
Pass the test and this pattern moves unknown
