0
0
Matplotlibdata~10 mins

Percentage labels 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 add percentage labels to the pie chart.

Matplotlib
import matplotlib.pyplot as plt
sizes = [30, 45, 25]
labels = ['A', 'B', 'C']
plt.pie(sizes, labels=labels, autopct=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A'%.1f%%'
B'%s%%'
C'%d%%'
D'%f%%'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '%d%%' which shows no decimal places.
Forgetting to include the percent sign '%%' in the format string.
2fill in blank
medium

Complete the code to display percentage labels with one decimal place on the pie chart.

Matplotlib
import matplotlib.pyplot as plt
sizes = [10, 20, 70]
labels = ['X', 'Y', 'Z']
plt.pie(sizes, labels=labels, autopct=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A'%d%%'
B'%s%%'
C'%.2f%%'
D'%.1f%%'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '%d%%' which shows no decimals.
Using '%s%%' which is not a valid number format.
3fill in blank
hard

Fix the error in the code to correctly show percentage labels on the pie chart.

Matplotlib
import matplotlib.pyplot as plt
sizes = [40, 35, 25]
labels = ['Red', 'Blue', 'Green']
plt.pie(sizes, labels=labels, autopct=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A'%.1f%%'
B'%1.f%%'
C'%.1f%'
D'%f%%'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '%1.f%%' which is invalid format.
Using '%.1f%' which misses escaping the percent sign.
4fill in blank
hard

Fill both blanks to create a pie chart with percentage labels showing two decimals and a shadow effect.

Matplotlib
import matplotlib.pyplot as plt
sizes = [50, 30, 20]
labels = ['Apple', 'Banana', 'Cherry']
plt.pie(sizes, labels=[1], autopct=[2], shadow=True)
plt.show()
Drag options to blanks, or click blank then click option'
Alabels
B['Apple', 'Banana', 'Cherry']
C'%.2f%%'
D'%.1f%%'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the variable name 'labels' instead of the list.
Using '%.1f%%' instead of '%.2f%%' for two decimals.
5fill in blank
hard

Fill all three blanks to create a pie chart with exploded slices, percentage labels with no decimals, and a start angle of 90 degrees.

Matplotlib
import matplotlib.pyplot as plt
sizes = [15, 25, 60]
labels = ['Cat', 'Dog', 'Rabbit']
explode = ([1], [2], [3])
plt.pie(sizes, labels=labels, autopct='%d%%', explode=explode, startangle=90)
plt.show()
Drag options to blanks, or click blank then click option'
A0.1
B0
C0.2
D0.3
Attempts:
3 left
💡 Hint
Common Mistakes
Using integers instead of floats for explode values.
Not matching the length of explode tuple to the number of slices.