Bird
Raised Fist0
Matplotlibdata~20 mins

Percentage labels in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 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.

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

  1. 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.
  2. Step 2: Compare with other parameters

    Other parameters like colors or title do not control percentage labels; only autopct does.
  3. Final Answer:

    It adds percentage labels to each slice of the pie chart. -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    plt.pie(data, autopct='%1.1f%%') -> Option C
  4. 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?
import matplotlib.pyplot as plt
sizes = [10, 20, 30]
plt.pie(sizes, autopct='%1.0f%%')
plt.show()
medium
A. 10%, 20%, 30%
B. 17%, 33%, 50%
C. 25%, 50%, 75%
D. 33%, 33%, 33%

Solution

  1. Step 1: Calculate total and percentages

    Total = 10 + 20 + 30 = 60. Percentages: 10/60=16.67%, 20/60=33.33%, 30/60=50%.
  2. Step 2: Apply autopct format '%1.0f%%'

    This rounds percentages to nearest whole number: 17%, 33%, 50%.
  3. Final Answer:

    17%, 33%, 50% -> Option B
  4. Quick Check:

    Rounded percentages = 17%, 33%, 50% [OK]
Hint: Sum slices, divide each, round with autopct [OK]
Common Mistakes:
  • Using raw values instead of percentages
  • Not rounding percentages correctly
  • Assuming equal slices without calculation
4. Identify the error in this code snippet that tries to add percentage labels to a pie chart:
import matplotlib.pyplot as plt
sizes = [40, 60]
plt.pie(sizes, autopct='%1.1f%')
plt.show()
medium
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

  1. 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.
  2. 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.
  3. Final Answer:

    The format string '%1.1f%' is missing an extra percent sign. -> Option A
  4. 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

  1. Step 1: Understand the requirement

    Show percentage labels only if slice percentage is greater than 20%, else show nothing.
  2. 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.
  3. Final Answer:

    lambda p: f'{p:.1f}%' if p > 20 else '' -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard