Donut chart variation in Matplotlib - Time & Space Complexity
We want to understand how the time to draw a donut chart changes as the amount of data grows.
How does adding more slices affect the work matplotlib does?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
sizes = [15, 30, 45, 10]
labels = ['A', 'B', 'C', 'D']
plt.pie(sizes, labels=labels, wedgeprops={'width':0.3})
plt.show()
This code draws a donut chart with 4 slices, each slice sized by the values in sizes.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Drawing each slice of the donut chart.
- How many times: Once for each slice in the
sizeslist.
As the number of slices increases, the work to draw each slice adds up.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 drawing steps |
| 100 | 100 drawing steps |
| 1000 | 1000 drawing steps |
Pattern observation: The work grows directly with the number of slices.
Time Complexity: O(n)
This means the time to draw the donut chart grows in a straight line as you add more slices.
[X] Wrong: "Drawing a donut chart always takes the same time no matter how many slices there are."
[OK] Correct: Each slice requires separate drawing steps, so more slices mean more work.
Understanding how drawing time grows with data size helps you explain performance in data visualization tasks clearly and confidently.
"What if we added animation to the donut chart? How would that affect the time complexity?"
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
