0
0
Matplotlibdata~20 mins

Donut chart variation in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Donut Chart Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple donut chart code
What will be the output of this code snippet that creates a donut chart using matplotlib?
Matplotlib
import matplotlib.pyplot as plt

sizes = [30, 20, 50]
labels = ['A', 'B', 'C']

plt.pie(sizes, labels=labels, wedgeprops=dict(width=0.4))
plt.show()
AA pie chart with three slices labeled A, B, C but no hole in the center
BAn error because wedgeprops is not a valid argument
CA bar chart with three bars labeled A, B, C
DA pie chart with three slices labeled A, B, C and a hole in the center (donut shape)
Attempts:
2 left
💡 Hint
Look at the wedgeprops argument and what width controls in a pie chart.
data_output
intermediate
1:30remaining
Number of wedges in a donut chart
Given this code, how many wedges (slices) will the donut chart display?
Matplotlib
import matplotlib.pyplot as plt

sizes = [10, 15, 25, 50]
labels = ['W', 'X', 'Y', 'Z']

plt.pie(sizes, labels=labels, wedgeprops=dict(width=0.3))
plt.show()
A4
B3
C1
D0
Attempts:
2 left
💡 Hint
Count the number of elements in the sizes list.
visualization
advanced
2:30remaining
Effect of changing width in donut chart
What visual difference will you see if you change the width in wedgeprops from 0.5 to 0.9 in this donut chart code?
Matplotlib
import matplotlib.pyplot as plt

sizes = [40, 30, 30]
labels = ['Red', 'Green', 'Blue']

plt.pie(sizes, labels=labels, wedgeprops=dict(width=0.5))
plt.show()
AThe hole in the center becomes larger, making the donut thinner
BThe chart changes to a normal pie chart with no hole
CThe hole in the center becomes smaller, making the donut thicker
DThe chart disappears because width 0.9 is invalid
Attempts:
2 left
💡 Hint
Width controls the thickness of the donut ring; higher width means thicker ring.
🔧 Debug
advanced
2:00remaining
Identify the error in donut chart code
What error will this code raise when trying to create a donut chart?
Matplotlib
import matplotlib.pyplot as plt

sizes = [25, 25, 25, 25]
labels = ['Q1', 'Q2', 'Q3', 'Q4']

plt.pie(sizes, labels=labels, wedgeprops={'width': 1.2})
plt.show()
AValueError because width cannot be greater than 1
BTypeError because wedgeprops must be a dict with string keys
CNo error, chart displays normally
DSyntaxError due to wrong dictionary syntax
Attempts:
2 left
💡 Hint
Width controls the thickness of the donut ring and must be between 0 and 1.
🚀 Application
expert
3:00remaining
Customize donut chart with exploded slice
Which code snippet correctly creates a donut chart with the second slice exploded outward?
Aplt.pie([20, 30, 50], labels=['X', 'Y', 'Z'], wedgeprops=dict(width=0.4), explode=[0.1, 0, 0])
Bplt.pie([20, 30, 50], labels=['X', 'Y', 'Z'], wedgeprops=dict(width=0.4), explode=[0, 0.1, 0])
Cplt.pie([20, 30, 50], labels=['X', 'Y', 'Z'], wedgeprops=dict(width=0.4), explode=[0, 0, 0.1])
Dplt.pie([20, 30, 50], labels=['X', 'Y', 'Z'], wedgeprops=dict(width=0.4), explode=[0.1, 0.1, 0])
Attempts:
2 left
💡 Hint
The explode list controls how far each slice is offset; the second slice is index 1.