What is the purpose of the plt.pie() function in matplotlib?
The plt.pie() function creates a pie chart, which is a circular graph divided into slices to show proportions of different categories.
Click to reveal answer
beginner
Which parameter in plt.pie() specifies the sizes of each slice?
The first parameter, usually a list or array, specifies the sizes (values) of each slice in the pie chart.
Click to reveal answer
beginner
How can you add labels to each slice in a pie chart using plt.pie()?
Use the labels parameter and pass a list of strings to name each slice.
Click to reveal answer
intermediate
What does the autopct parameter do in plt.pie()?
The autopct parameter formats and displays the percentage value on each slice, e.g., '%.1f%%' shows one decimal place.
Click to reveal answer
intermediate
Why is it important to call plt.axis('equal') after plt.pie()?
Calling plt.axis('equal') makes sure the pie chart is drawn as a circle instead of an ellipse, keeping proportions accurate.
Click to reveal answer
What type of chart does plt.pie() create?
APie chart
BBar chart
CLine chart
DScatter plot
✗ Incorrect
plt.pie() creates a pie chart, which shows parts of a whole as slices.
Which parameter adds labels to pie chart slices?
Aexplode
Bsizes
Ccolors
Dlabels
✗ Incorrect
The labels parameter adds names to each slice.
How do you show percentage values on pie slices?
AUse <code>labels</code>
BUse <code>autopct</code>
CUse <code>colors</code>
DUse <code>explode</code>
✗ Incorrect
The autopct parameter formats and shows percentages on slices.
What does plt.axis('equal') do after plotting a pie chart?
AChanges colors
BExplodes slices
CMakes pie circular
DAdds labels
✗ Incorrect
It ensures the pie chart is a perfect circle, not an ellipse.
Which of these is NOT a parameter of plt.pie()?
Axlabel
Bsizes
Cautopct
Dlabels
✗ Incorrect
xlabel is for axis labels, not used in pie charts.
Explain how to create a basic pie chart using plt.pie() including how to add labels and percentages.
Think about the main inputs needed to show parts of a whole clearly.
You got /4 concepts.
Why is it important to use plt.axis('equal') after plotting a pie chart?
Consider the shape of the pie chart and how it affects visual accuracy.
You got /3 concepts.
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
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.
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().
Final Answer:
It creates a pie chart to show parts of a whole. -> Option B
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
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=.
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.
Final Answer:
plt.pie(sizes, labels=labels) -> Option A
Quick Check:
Sizes first, labels keyword [OK]
Hint: Sizes first, labels with labels= keyword [OK]
D. The sizes argument should be the first positional argument, not labels.
Solution
Step 1: Check argument order in plt.pie()
The first argument must be sizes (numbers), but the code passes labels first, which is incorrect.
Step 2: Correct argument usage
Sizes should be the first positional argument, and labels should be passed with labels=labels keyword.
Final Answer:
The sizes argument should be the first positional argument, not labels. -> Option D
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
Step 1: Identify the largest slice
The sizes are [15, 25, 35, 25]. The largest value is 35 at index 2 (third element).
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.
Final Answer:
plt.pie([15,25,35,25], labels=['P1','P2','P3','P4'], explode=[0,0,0.1,0], autopct='%1.1f%%') -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.