A donut chart helps show parts of a whole, like a pie chart, but with a hole in the middle. This makes it easier to read and looks nice.
Donut chart variation in Matplotlib
import matplotlib.pyplot as plt sizes = [30, 20, 25, 25] labels = ['A', 'B', 'C', 'D'] plt.pie(sizes, labels=labels, wedgeprops=dict(width=0.4)) plt.show()
The wedgeprops argument controls the thickness of the donut ring.
Setting width less than 1 creates the hole in the middle.
plt.pie([40, 60], labels=['Yes', 'No'], wedgeprops=dict(width=0.3)) plt.show()
plt.pie([10, 20, 30], labels=['X', 'Y', 'Z'], wedgeprops=dict(width=0.5), startangle=90) plt.show()
plt.pie([25, 25, 25, 25], labels=['Q1', 'Q2', 'Q3', 'Q4'], wedgeprops=dict(width=0.4), autopct='%1.1f%%') plt.show()
This program creates a donut chart showing fruit distribution with colors and percentages.
import matplotlib.pyplot as plt # Data for the chart sizes = [15, 30, 45, 10] labels = ['Apples', 'Bananas', 'Cherries', 'Dates'] colors = ['#ff9999','#66b3ff','#99ff99','#ffcc99'] # Create donut chart plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=140, wedgeprops=dict(width=0.5)) # Add a title plt.title('Fruit Distribution') # Show the plot plt.show()
Donut charts are easier to read when the hole is not too small or too large.
Use autopct to add percentage labels inside slices.
Colors help distinguish categories clearly.
Donut charts are pie charts with a hole in the center.
Use wedgeprops=dict(width=...) to create the hole.
They help show parts of a whole in a clean, readable way.
Practice
What is the main difference between a donut chart and a pie chart in matplotlib?
Solution
Step 1: Understand pie chart basics
A pie chart divides a circle into slices representing parts of a whole.Step 2: Identify donut chart feature
A donut chart is a pie chart with a hole in the center, making it look like a donut.Final Answer:
A donut chart has a hole in the center, a pie chart does not. -> Option AQuick Check:
Donut chart = pie chart + hole [OK]
- Thinking donut charts use bars
- Confusing donut charts with 3D charts
- Believing donut charts show time series
Which matplotlib code snippet correctly creates a donut chart with a hole size controlled by wedgeprops?
import matplotlib.pyplot as plt sizes = [30, 40, 30] plt.pie(sizes, wedgeprops=???) plt.show()
Solution
Step 1: Recall donut hole control
The hole size in a donut chart is set by the 'width' key inside wedgeprops.Step 2: Match correct syntax
Using wedgeprops={'width': 0.5} creates a hole half the radius size.Final Answer:
wedgeprops={'width': 0.5} -> Option CQuick Check:
wedgeprops width controls hole size [OK]
- Using 'radius' instead of 'width'
- Using 'height' or 'size' keys incorrectly
- Omitting wedgeprops entirely
What will be the visual output of this code?
import matplotlib.pyplot as plt
sizes = [10, 20, 30]
plt.pie(sizes, wedgeprops={'width': 0.3})
plt.show()Solution
Step 1: Analyze wedgeprops width value
The width is set to 0.3, meaning the donut hole is 70% of the radius.Step 2: Visualize the chart shape
This creates a donut chart with a thin ring shape because the hole is large but not full radius.Final Answer:
A donut chart with a thin ring shape -> Option AQuick Check:
width=0.3 means thin donut ring [OK]
- Thinking width=0.3 means no hole
- Confusing hole size with radius
- Expecting a bar chart output
Identify the error in this donut chart code and select the fix:
import matplotlib.pyplot as plt
sizes = [25, 35, 40]
plt.pie(sizes, wedgeprops={'width': '0.5'})
plt.show()Solution
Step 1: Check wedgeprops value type
The width value is given as a string '0.5', but it must be a number (float).Step 2: Correct the data type
Changing '0.5' to 0.5 fixes the type error and creates the donut hole properly.Final Answer:
Change '0.5' to 0.5 (string to float) -> Option DQuick Check:
width must be float, not string [OK]
- Using string instead of float for width
- Changing width key incorrectly
- Ignoring type errors
You want to create a donut chart showing sales distribution with a hole size that leaves 40% thickness for the slices. Which wedgeprops value should you use?
Solution
Step 1: Understand width meaning
The 'width' value is the thickness of the slices as a fraction of the radius.Step 2: Calculate width for 40% thickness
For 40% thickness for the slices, set width=0.6 (leaving a hole size of 40%).Final Answer:
wedgeprops={'width': 0.6} -> Option BQuick Check:
width=0.6 gives 40% slice thickness [OK]
- Confusing width with hole size
- Using width less than hole thickness
- Setting width to 1 (no hole)
Ready to prove it?
Pass the test and this pattern moves unknown
