Bird
Raised Fist0
Matplotlibdata~20 mins

Basic pie chart with plt.pie in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pie Chart Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this pie chart code?
Consider this Python code using matplotlib to create a pie chart. What will be the displayed percentages on the pie slices?
Matplotlib
import matplotlib.pyplot as plt
sizes = [10, 20, 30]
labels = ['A', 'B', 'C']
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.show()
ASlices labeled A: 16.7%, B: 33.3%, C: 50.0%
BSlices labeled A: 25%, B: 25%, C: 50%
CSlices labeled A: 10%, B: 20%, C: 30%
DSlices labeled A: 20%, B: 30%, C: 50%
Attempts:
2 left
💡 Hint
Percentages are calculated as each size divided by the total sum of sizes.
❓ data_output
intermediate
1:30remaining
How many slices will the pie chart have?
Given this code snippet, how many slices will the pie chart display?
Matplotlib
import matplotlib.pyplot as plt
sizes = [5, 15, 25, 55]
plt.pie(sizes)
plt.show()
A1 slice
B5 slices
C3 slices
D4 slices
Attempts:
2 left
💡 Hint
Each number in sizes corresponds to one slice.
❓ visualization
advanced
2:30remaining
Which option produces a pie chart with exploded slices?
Look at these four code snippets. Which one will create a pie chart where each slice is slightly separated (exploded) from the center?
Aplt.pie([10, 20, 30], explode=[0, 0, 0])
Bplt.pie([10, 20, 30], explode=[1, 0, 0])
Cplt.pie([10, 20, 30], explode=[0.1, 0.1, 0.1])
Dplt.pie([10, 20, 30], explode=[0.1, 0, 0.1])
Attempts:
2 left
💡 Hint
The explode parameter controls how far each slice is offset from the center.
🔧 Debug
advanced
2:00remaining
What error does this pie chart code raise?
This code tries to create a pie chart but fails. What error will it raise?
Matplotlib
import matplotlib.pyplot as plt
sizes = [10, 20]
labels = ['A', 'B', 'C']
plt.pie(sizes, labels=labels)
plt.show()
ATypeError: unsupported operand type(s) for +: 'int' and 'str'
BValueError: 'labels' must be the same length as 'sizes'
CIndexError: list index out of range
DNo error, pie chart displays with missing label
Attempts:
2 left
💡 Hint
Labels list length must match sizes list length exactly.
🚀 Application
expert
2:30remaining
Which option produces a pie chart with a shadow and start angle at 90 degrees?
You want a pie chart that has a shadow effect and starts drawing slices from the top (90 degrees). Which code snippet does this correctly?
Aplt.pie([15, 25, 60], shadow=True, startangle=90)
Bplt.pie([15, 25, 60], shadow=False, startangle=0)
Cplt.pie([15, 25, 60], shadow=True, startangle=0)
Dplt.pie([15, 25, 60], shadow=False, startangle=90)
Attempts:
2 left
💡 Hint
The shadow parameter adds a shadow, startangle rotates the start position in degrees.

Practice

(1/5)
1. What does the plt.pie() function in matplotlib do?
easy
A. It creates a line chart to show trends over time.
B. It creates a pie chart to show parts of a whole.
C. It creates a bar chart to compare categories.
D. It creates a scatter plot to show data points.

Solution

  1. Step 1: Understand the purpose of plt.pie()

    The function plt.pie() is designed to create pie charts, which visually represent parts of a whole as slices of a circle.
  2. Step 2: Compare with other chart types

    Line charts, bar charts, and scatter plots are created with different functions like plt.plot(), plt.bar(), and plt.scatter(), not plt.pie().
  3. Final Answer:

    It creates a pie chart to show parts of a whole. -> Option B
  4. Quick Check:

    Pie chart = plt.pie() [OK]
Hint: Pie chart = parts of whole, use plt.pie() [OK]
Common Mistakes:
  • Confusing pie chart with bar or line charts
  • Thinking plt.pie() creates scatter plots
  • Mixing up chart functions in matplotlib
2. Which of the following is the correct syntax to create a basic pie chart with labels using matplotlib?
easy
A. plt.pie(sizes, labels=labels)
B. plt.pie(labels, sizes=sizes)
C. plt.pie(labels=sizes, sizes=labels)
D. plt.pie(sizes, labels)

Solution

  1. Step 1: Check the order of arguments in plt.pie()

    The first argument to plt.pie() is the sizes (values) of slices, and labels are passed as a keyword argument labels=.
  2. Step 2: Validate correct keyword usage

    plt.pie(sizes, labels=labels) correctly uses plt.pie(sizes, labels=labels). Options B and C misuse argument order or keywords, and D misses the keyword for labels.
  3. Final Answer:

    plt.pie(sizes, labels=labels) -> Option A
  4. Quick Check:

    Sizes first, labels keyword [OK]
Hint: Sizes first, labels with labels= keyword [OK]
Common Mistakes:
  • Passing labels as first argument instead of sizes
  • Omitting labels= keyword
  • Swapping sizes and labels arguments
3. What will be the output of this code?
import matplotlib.pyplot as plt
sizes = [30, 20, 50]
labels = ['A', 'B', 'C']
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.show()
medium
A. A pie chart with three slices labeled A, B, C showing percentages 30.0%, 20.0%, 50.0%
B. A bar chart with bars labeled A, B, C
C. A pie chart with slices but no labels or percentages
D. Syntax error due to autopct usage

Solution

  1. Step 1: Understand plt.pie() parameters

    The sizes list defines slice sizes, labels assign names, and autopct='%1.1f%%' adds percentage text on slices.
  2. Step 2: Predict the visual output

    The code will produce a pie chart with three slices labeled A, B, C, each showing their percentage values 30.0%, 20.0%, and 50.0% respectively.
  3. Final Answer:

    A pie chart with three slices labeled A, B, C showing percentages 30.0%, 20.0%, 50.0% -> Option A
  4. Quick Check:

    autopct adds percentages [OK]
Hint: autopct='%1.1f%%' shows slice percentages [OK]
Common Mistakes:
  • Thinking autopct causes errors
  • Expecting bar chart instead of pie chart
  • Ignoring labels or percentages on chart
4. Identify the error in this code snippet that tries to create a pie chart:
import matplotlib.pyplot as plt
sizes = [40, 30, 30]
labels = ['X', 'Y', 'Z']
plt.pie(labels, sizes=sizes)
plt.show()
medium
A. The sizes list must be strings, not numbers.
B. The labels list is missing a keyword argument.
C. plt.show() is missing parentheses.
D. The sizes argument should be the first positional argument, not labels.

Solution

  1. Step 1: Check argument order in plt.pie()

    The first argument must be sizes (numbers), but the code passes labels first, which is incorrect.
  2. Step 2: Correct argument usage

    Sizes should be the first positional argument, and labels should be passed with labels=labels keyword.
  3. Final Answer:

    The sizes argument should be the first positional argument, not labels. -> Option D
  4. Quick Check:

    Sizes first, labels keyword [OK]
Hint: Sizes first, labels with labels= keyword [OK]
Common Mistakes:
  • Passing labels as first argument
  • Forgetting labels= keyword
  • Confusing argument order
5. You want to create a pie chart showing sales distribution for 4 products with sizes [15, 25, 35, 25] and labels ['P1', 'P2', 'P3', 'P4']. You also want to highlight the largest slice by "exploding" it out. Which code correctly does this?
hard
A. plt.pie([15,25,35,25], labels=['P1','P2','P3','P4'], explode=[0,0,0,0.1], autopct='%1.1f%%')
B. plt.pie([15,25,35,25], labels=['P1','P2','P3','P4'], explode=[0.1,0,0,0], autopct='%1.1f%%')
C. plt.pie([15,25,35,25], labels=['P1','P2','P3','P4'], explode=[0,0,0.1,0], autopct='%1.1f%%')
D. plt.pie([15,25,35,25], labels=['P1','P2','P3','P4'], explode=[0,0.1,0,0], autopct='%1.1f%%')

Solution

  1. Step 1: Identify the largest slice

    The sizes are [15, 25, 35, 25]. The largest value is 35 at index 2 (third element).
  2. Step 2: Set explode to highlight largest slice

    The explode list should have 0.1 at index 2 and 0 elsewhere: [0, 0, 0.1, 0]. plt.pie([15,25,35,25], labels=['P1','P2','P3','P4'], explode=[0,0,0.1,0], autopct='%1.1f%%') matches this.
  3. Final Answer:

    plt.pie([15,25,35,25], labels=['P1','P2','P3','P4'], explode=[0,0,0.1,0], autopct='%1.1f%%') -> Option C
  4. Quick Check:

    Explode largest slice index 2 [OK]
Hint: Explode list: 0.1 at largest slice index [OK]
Common Mistakes:
  • Exploding wrong slice index
  • Using explode without matching list length
  • Forgetting autopct for percentages
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