Lesson 19
Pie Charts
Show how a whole splits into parts with plt.pie, label every slice, print the percentages with autopct, and know when a bar chart reads better.
A pie chart shows one whole cut into parts. Each slice gets an angle in proportion to its share: a value's share of the total, times 360 degrees. plt.pie does that arithmetic for you, so you only hand it the raw values and the labels.
From values to angles
You never convert to percentages yourself. Matplotlib divides each value by the sum and turns the share into an angle. Working it out once by hand shows you what the picture means.
tracks = ["Python", "NumPy", "pandas", "Plotting"]
enrolled = [120, 86, 94, 58]
total = sum(enrolled)
for m, p in zip(tracks, enrolled):
share = p / total
print(f"{m:<9} {share:6.1%} {share * 360:5.1f} degrees")
print("total:", total)Output:
Python 33.5% 120.7 degrees
NumPy 24.0% 86.5 degrees
pandas 26.3% 94.5 degrees
Plotting 16.2% 58.3 degrees
total: 358A first pie
Pass the values first and the labels with labels=. The first slice starts at the three o'clock position and the rest follow counter-clockwise. plt.axis("equal") is no longer needed in current Matplotlib, because pie already keeps the circle round.
import matplotlib.pyplot as plt
tracks = ["Python", "NumPy", "pandas", "Plotting"]
enrolled = [120, 86, 94, 58]
plt.pie(enrolled, labels=tracks)
plt.title("Enrolments by track")
plt.show()
print("slices:", len(enrolled))Output:
slices: 4Percentages with autopct
autopct writes each slice's share on the slice. It takes a format string: "%1.1f%%" means one decimal place, and the doubled %% prints a literal percent sign. Without the doubling you would get an error or a missing symbol.
import matplotlib.pyplot as plt
tracks = ["Python", "NumPy", "pandas", "Plotting"]
enrolled = [120, 86, 94, 58]
plt.pie(enrolled, labels=tracks, autopct="%1.1f%%")
plt.title("Share of enrolments")
plt.show()
print("%1.1f%%" % (120 / sum(enrolled) * 100))Output:
33.5%The printed line is exactly the text that lands on the Python slice, which is a quick way to check a format string before you draw.
Pick your own colours
colors= takes one colour per slice, in the same order as the values. A small, consistent palette keeps neighbouring slices easy to tell apart.
import matplotlib.pyplot as plt
tracks = ["Python", "NumPy", "pandas", "Plotting"]
enrolled = [120, 86, 94, 58]
palette = ["#7C3AED", "#2563EB", "#EC4899", "#F59E0B"]
plt.pie(enrolled, labels=tracks, colors=palette, autopct="%1.0f%%")
plt.title("Enrolments, in the MahiDev palette")
plt.show()
print(len(palette) == len(enrolled))Output:
TrueWhen not to use a pie
Eyes compare lengths far better than angles. With more than five or six slices, or with slices of nearly equal size, a sorted bar chart tells the same story more clearly. Keep pies for a few parts of one whole that really do add up to 100 percent.
import matplotlib.pyplot as plt
tracks = ["Python", "NumPy", "pandas", "Plotting"]
enrolled = [120, 86, 94, 58]
order = sorted(zip(enrolled, tracks))
values = [m for m, p in order]
names = [p for m, p in order]
plt.barh(names, values, color="#7C3AED")
plt.title("The same data, sorted as bars")
plt.xlabel("learners")
plt.show()
print(names)Output:
['Plotting', 'NumPy', 'pandas', 'Python']Key takeaways
-
A slice's angle is its share of the total times 360 degrees
-
plt.pie takes raw values; labels= names the slices
-
autopct="%1.1f%%" prints shares, and %% is a literal percent sign
-
Use a sorted bar chart when there are many or similar-sized parts
-
Next: Styling a Pie Chart
-
Previous: Histogram Styles
Loading playground…
Check your understanding
Pie Charts Quiz
What does a pie chart show?
1 / 10