Complete the code to create a basic pie chart using matplotlib.
import matplotlib.pyplot as plt sizes = [30, 20, 50] labels = ['A', 'B', 'C'] plt.pie(sizes, labels=[1]) plt.show()
The labels parameter is used to add labels to each slice of the pie chart.
Complete the code to add a white circle in the center to create a donut chart effect.
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()
The radius of the white circle should be less than 1 to create the donut hole effect. 0.5 is a common choice.
Fix the error in the code to correctly display the donut chart with percentage labels.
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()
The autopct string '%1.1f%%' formats the percentage with one decimal place and a percent sign.
Fill both blanks to create a donut chart with exploded slices and a shadow effect.
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()
Exploding the first slice by 0.1 separates it slightly. Setting shadow to True adds a shadow effect.
Fill all three blanks to create a donut chart with custom colors, start angle, and percentage format.
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()
The third color is a light green '#99ff99'. The start angle 90 rotates the chart. The autopct '%1.0f%%' shows percentages with no decimals.