Challenge - 5 Problems
Percentage Labels Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pie chart with percentage labels
What will be the output of this code snippet that creates a pie chart with percentage labels?
Matplotlib
import matplotlib.pyplot as plt sizes = [15, 30, 45, 10] labels = ['A', 'B', 'C', 'D'] plt.pie(sizes, labels=labels, autopct='%1.1f%%') plt.show()
Attempts:
2 left
💡 Hint
Look at the autopct parameter format string '%1.1f%%' to understand the percentage format.
✗ Incorrect
The autopct='%1.1f%%' formats the percentage with one decimal place and adds a percent sign. Labels are shown as given in the labels list.
🧠 Conceptual
intermediate1:30remaining
Understanding autopct parameter in pie charts
What does the autopct parameter do in matplotlib's pie chart function?
Attempts:
2 left
💡 Hint
Think about what 'pct' in autopct might stand for.
✗ Incorrect
The autopct parameter is used to display the percentage value on each slice of the pie chart, formatted as specified.
🔧 Debug
advanced2:00remaining
Identify the error in percentage label formatting
What error will this code produce when trying to add percentage labels to a pie chart?
import matplotlib.pyplot as plt
sizes = [10, 20, 30]
labels = ['X', 'Y', 'Z']
plt.pie(sizes, labels=labels, autopct='%1.f%%')
plt.show()
Attempts:
2 left
💡 Hint
Check if '%1.f%%' is a valid Python format specifier for floats.
✗ Incorrect
The format specifier '%1.f%%' is invalid because after the decimal point '.', a precision digit is required (e.g., '%1.0f%%'). This causes a ValueError.
❓ data_output
advanced2:30remaining
Resulting labels with custom autopct function
Given this code, what is the output of the labels shown on the pie chart slices?
import matplotlib.pyplot as plt
sizes = [25, 25, 50]
labels = ['Red', 'Blue', 'Green']
def func(pct):
return f'{pct:.0f}%% of total'
plt.pie(sizes, labels=labels, autopct=func)
plt.show()
Attempts:
2 left
💡 Hint
Look at the custom function formatting the percentage string.
✗ Incorrect
The custom function formats the percentage with no decimals and adds ' of total' text after the percent sign.
🚀 Application
expert3:00remaining
Creating a pie chart with percentage labels and exploded slice
Which code snippet correctly creates a pie chart with percentage labels showing one decimal place, labels for each slice, and the largest slice exploded outward?
Attempts:
2 left
💡 Hint
The largest slice is 40, so explode should highlight that slice. Check the autopct format for one decimal place and percent sign.
✗ Incorrect
Option B explodes the largest slice (index 3) with 0.1 offset, uses autopct='%1.1f%%' to show one decimal place and percent sign correctly.