SwadhyAI Notes

All Python notes

Matplotlib Pie Charts 🥧

Pie Charts

Use pie() to create pie charts. The size of each wedge is proportional to its value relative to the sum of all values.

✨ Basic Pie Chart

python
import matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = mylabels)
plt.show()

💥 Explode & Shadow

Make a slice stand out using explode and add depth with shadow.

python
myexplode = [0.2, 0, 0, 0] # Pull out the first slice

plt.pie(y, labels = mylabels, explode = myexplode, shadow = True)

🎨 Colors & Legend

Customize colors and add a legend.

python
mycolors = ["black", "hotpink", "b", "#4CAF50"]

plt.pie(y, labels = mylabels, colors = mycolors)
plt.legend(title = "Four Fruits")
plt.show()

✨ Pro Tip: Start Angle

By default, the first wedge starts at the x-axis (0 degrees). Change this with startangle.

python
plt.pie(y, labels = mylabels, startangle = 90)