0
0
Matplotlibdata~10 mins

Donut chart variation in Matplotlib - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a basic pie chart using matplotlib.

Matplotlib
import matplotlib.pyplot as plt

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

plt.pie(sizes, labels=[1])
plt.show()
Drag options to blanks, or click blank then click option'
Asizes
Bcolors
Clabels
Dexplode
Attempts:
3 left
💡 Hint
Common Mistakes
Using sizes instead of labels for the labels parameter.
Passing a variable that does not exist.
2fill in blank
medium

Complete the code to add a white circle in the center to create a donut chart effect.

Matplotlib
import matplotlib.pyplot as plt

sizes = [40, 35, 25]
labels = ['X', 'Y', 'Z']

plt.pie(sizes, labels=labels)
centre_circle = plt.Circle((0,0), [1], fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
plt.show()
Drag options to blanks, or click blank then click option'
A1.5
B0.5
C0
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using a radius equal or greater than 1 which covers the whole pie.
Using zero radius which makes no hole.
3fill in blank
hard

Fix the error in the code to correctly display the donut chart with percentage labels.

Matplotlib
import matplotlib.pyplot as plt

sizes = [25, 50, 25]
labels = ['Red', 'Green', 'Blue']

plt.pie(sizes, labels=labels, autopct=[1])
centre_circle = plt.Circle((0,0), 0.6, fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
plt.show()
Drag options to blanks, or click blank then click option'
A'%1.1f%%'
BNone
C'%.1f%'
D'%1.f%%'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '%1.f%%' which misses the decimal place.
Using '%.1f%' which misses the second percent sign.
4fill in blank
hard

Fill both blanks to create a donut chart with exploded slices and a shadow effect.

Matplotlib
import matplotlib.pyplot as plt

sizes = [15, 30, 55]
labels = ['Apple', 'Banana', 'Cherry']
explode = ([1], 0, 0)

plt.pie(sizes, labels=labels, explode=explode, shadow=[2])
centre_circle = plt.Circle((0,0), 0.7, fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
plt.show()
Drag options to blanks, or click blank then click option'
A0.1
BTrue
CFalse
D0.5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of a positive float for explode.
Setting shadow to a string instead of a boolean.
5fill in blank
hard

Fill all three blanks to create a donut chart with custom colors, start angle, and percentage format.

Matplotlib
import matplotlib.pyplot as plt

sizes = [20, 40, 40]
labels = ['East', 'West', 'North']
colors = ['#ff9999', '#66b3ff', [1]]

plt.pie(sizes, labels=labels, colors=colors, startangle=[2], autopct=[3])
centre_circle = plt.Circle((0,0), 0.5, fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
plt.show()
Drag options to blanks, or click blank then click option'
A'#99ff99'
B90
C'%1.0f%%'
D'#333333'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a color string without quotes.
Using a start angle as a string instead of an integer.
Using autopct without the double percent signs.