0
0
Matplotlibdata~20 mins

Percentage labels in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Percentage Labels Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()
AA pie chart with four slices labeled A, B, C, D and percentages 15%, 30%, 45%, 10% without decimal places
BA pie chart with four slices labeled A, B, C, D but no percentage labels shown
CA pie chart with four slices labeled A, B, C, D and percentages 15.0%, 30.0%, 45.0%, 10.0% displayed on each slice
DA pie chart with four slices but labels are numbers 0, 1, 2, 3 instead of A, B, C, D
Attempts:
2 left
💡 Hint
Look at the autopct parameter format string '%1.1f%%' to understand the percentage format.
🧠 Conceptual
intermediate
1:30remaining
Understanding autopct parameter in pie charts
What does the autopct parameter do in matplotlib's pie chart function?
AIt formats and displays the percentage value on each pie slice
BIt sets the color of each pie slice
CIt controls the size of the pie chart
DIt adds a legend to the pie chart
Attempts:
2 left
💡 Hint
Think about what 'pct' in autopct might stand for.
🔧 Debug
advanced
2: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()
AValueError because '%1.f%%' is an invalid format specifier
BNo error; the pie chart shows percentages without decimal places
CTypeError because autopct expects a function, not a string
DSyntaxError due to incorrect format string in autopct
Attempts:
2 left
💡 Hint
Check if '%1.f%%' is a valid Python format specifier for floats.
data_output
advanced
2: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()
ASlices labeled 'Red', 'Blue', 'Green' with percentages '25.0%', '25.0%', '50.0%'
BSlices labeled 'Red', 'Blue', 'Green' with percentages '25%% of total', '25%% of total', '50%% of total'
CSlices labeled 'Red', 'Blue', 'Green' with no percentage labels
DSlices labeled 'Red', 'Blue', 'Green' with percentages '25% of total', '25% of total', '50% of total'
Attempts:
2 left
💡 Hint
Look at the custom function formatting the percentage string.
🚀 Application
expert
3: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?
A
import matplotlib.pyplot as plt
sizes = [10, 20, 30, 40]
labels = ['A', 'B', 'C', 'D']
explode = [0.1, 0, 0, 0]
plt.pie(sizes, labels=labels, autopct='%1.1f%%', explode=explode)
plt.show()
B
import matplotlib.pyplot as plt
sizes = [10, 20, 30, 40]
labels = ['A', 'B', 'C', 'D']
explode = [0, 0, 0, 0.1]
plt.pie(sizes, labels=labels, autopct='%1.1f%%', explode=explode)
plt.show()
C
import matplotlib.pyplot as plt
sizes = [10, 20, 30, 40]
labels = ['A', 'B', 'C', 'D']
explode = [0, 0, 0, 0.1]
plt.pie(sizes, labels=labels, autopct='%1.0f%%', explode=explode)
plt.show()
D
import matplotlib.pyplot as plt
sizes = [10, 20, 30, 40]
labels = ['A', 'B', 'C', 'D']
explode = [0, 0, 0, 0.1]
plt.pie(sizes, labels=labels, autopct='%1.1f', explode=explode)
plt.show()
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.