0
0
Matplotlibdata~10 mins

Percentage labels in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Percentage labels
Start: Data and Values
Create Pie Chart
Calculate Percentages
Add Percentage Labels
Display Chart with Labels
End
The flow shows how data values are used to create a pie chart, calculate percentages, add labels, and display the final chart.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
sizes = [15, 30, 45, 10]
plt.pie(sizes, labels=['A', 'B', 'C', 'D'], autopct='%1.1f%%')
plt.show()
This code creates a pie chart with four slices and adds percentage labels on each slice.
Execution Table
StepActionInput/StateOutput/Result
1Define sizes listsizes = [15, 30, 45, 10]sizes stored
2Call plt.pie()sizes and labels=['A','B','C','D'], autopct='%1.1f%%'Pie chart slices created with labels
3Calculate totalSum of sizes = 100Total = 100
4Calculate percentage for each sliceEach size / total * 100Percentages: 15%, 30%, 45%, 10%
5Format percentage labelsUsing '%1.1f%%'Labels: '15.0%', '30.0%', '45.0%', '10.0%'
6Add labels to pie slicesLabels and percentagesPie chart slices labeled with percentages
7Display chartPie chart with labelsPie chart shown on screen
8EndChart displayedExecution complete
💡 All slices labeled and chart displayed, execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
sizesundefined[15, 30, 45, 10][15, 30, 45, 10][15, 30, 45, 10][15, 30, 45, 10]
totalundefinedundefined100100100
percentagesundefinedundefinedundefined[15.0, 30.0, 45.0, 10.0][15.0, 30.0, 45.0, 10.0]
labelsundefinedundefinedundefinedundefined['15.0%', '30.0%', '45.0%', '10.0%']
Key Moments - 3 Insights
Why do the percentage labels add up to 100% even if the sizes are arbitrary numbers?
Because the code calculates the total sum of all sizes (step 3) and then computes each slice's percentage relative to that total (step 4), ensuring the labels represent parts of the whole.
What does the format string '%1.1f%%' mean in autopct?
It formats the percentage to show one digit before and one digit after the decimal point, followed by a percent sign, as seen in step 5 of the execution table.
Can the autopct parameter be a function instead of a format string?
Yes, autopct can be a function that receives the percentage and returns a custom label string, but here we use a format string for simplicity (step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the total sum of sizes calculated at step 3?
A90
B120
C100
DNone
💡 Hint
Check the 'Calculate total' row in the execution_table at step 3.
At which step are the percentage labels formatted as strings like '15.0%'?
AStep 5
BStep 4
CStep 2
DStep 6
💡 Hint
Look for the step where formatting with '%1.1f%%' is applied in the execution_table.
If the sizes list changed to [10, 20, 30, 40], what would happen to the percentages in the variable_tracker?
AThey would remain the same as [15.0, 30.0, 45.0, 10.0]
BThey would change to [10.0, 20.0, 30.0, 40.0] and sum to 100
CThey would change to [10.0, 20.0, 30.0, 40.0]
DThey would change to [10.0, 20.0, 30.0, 40.0] but not sum to 100
💡 Hint
Percentages are calculated as each size divided by total sum times 100, so they always sum to 100 (see variable_tracker).
Concept Snapshot
Percentage labels in matplotlib pie charts:
- Use plt.pie() with autopct parameter
- autopct='%1.1f%%' formats labels as percentages
- Percentages are calculated from slice sizes
- Labels show on each pie slice automatically
- Useful for visualizing parts of a whole
Full Transcript
This visual execution traces how matplotlib creates a pie chart with percentage labels. First, a list of sizes is defined. Then plt.pie() is called with labels and autopct format string. The total of sizes is calculated to find the whole. Each slice's percentage is computed by dividing its size by the total and multiplying by 100. These percentages are formatted as strings like '15.0%'. The labels are added to the pie slices. Finally, the chart is displayed. Key points include how percentages always sum to 100 and how the autopct string controls label formatting.