Lesson 20
Styling a Pie Chart
Pull one slice out with explode, rotate the pie with startangle, add a shadow or a donut hole, and put the labels in a legend instead.
A plain pie gives every slice equal attention. The styling options let you point at the one slice that matters, turn the pie so it reads from the top, and move crowded labels out of the way.
Pull a slice out with explode
explode takes one offset per slice, as a fraction of the radius. Zero leaves a slice in place and a small value such as 0.1 slides it outwards. Explode one slice, not all of them, or nothing stands out.
import matplotlib.pyplot as plt
tracks = ["Python", "NumPy", "pandas", "Plotting"]
enrolled = [120, 86, 94, 58]
explode = [0, 0, 0.12, 0]
plt.pie(enrolled, labels=tracks, explode=explode, autopct="%1.0f%%")
plt.title("pandas, pulled forward")
plt.show()
print(dict(zip(tracks, explode)))Output:
{'Python': 0, 'NumPy': 0, 'pandas': 0.12, 'Plotting': 0}Rotate with startangle
startangle turns the whole pie, in degrees counter-clockwise from three o'clock. startangle=90 starts the first slice at twelve o'clock, which is how most people expect to read a pie. counterclock=False makes the slices run clockwise instead.
import matplotlib.pyplot as plt
tracks = ["Python", "NumPy", "pandas", "Plotting"]
enrolled = [120, 86, 94, 58]
plt.pie(enrolled, labels=tracks, startangle=90, counterclock=False,
autopct="%1.0f%%")
plt.title("Reading clockwise from the top")
plt.show()
print("start at 90 degrees")Output:
start at 90 degreesA donut with wedgeprops
wedgeprops passes styling to every slice. Setting a width smaller than 1 leaves a hole in the middle, which gives you room for a total. edgecolor draws a thin gap between slices.
import matplotlib.pyplot as plt
tracks = ["Python", "NumPy", "pandas", "Plotting"]
enrolled = [120, 86, 94, 58]
plt.pie(enrolled, labels=tracks, startangle=90,
wedgeprops={"width": 0.4, "edgecolor": "white"})
plt.text(0, 0, str(sum(enrolled)), ha="center", va="center", fontsize=18)
plt.title("A donut with the total in the middle")
plt.show()
print("total in the hole:", sum(enrolled))Output:
total in the hole: 358Labels in a legend
When labels crowd each other, leave them off the slices and draw a legend. pie returns the wedges it drew, and plt.legend pairs them with your names. bbox_to_anchor moves the legend beside the pie so it covers nothing.
import matplotlib.pyplot as plt
tracks = ["Python", "NumPy", "pandas", "Plotting"]
enrolled = [120, 86, 94, 58]
wedges, texts, autotexts = plt.pie(enrolled, autopct="%1.0f%%", startangle=90)
plt.legend(wedges, tracks, title="track", loc="center left",
bbox_to_anchor=(1, 0.5))
plt.title("Legend beside the pie")
plt.show()
print(len(wedges), "wedges")Output:
4 wedgesWith autopct set, pie returns three lists: the wedges, the label texts, and the percentage texts. Without it, only the first two.
A shadow, used sparingly
shadow=True adds a soft shadow under the pie. It looks dramatic but adds nothing to the data, so keep it for slides rather than reports.
import matplotlib.pyplot as plt
tracks = ["Python", "NumPy", "pandas", "Plotting"]
enrolled = [120, 86, 94, 58]
plt.pie(enrolled, labels=tracks, shadow=True, startangle=90,
explode=[0.08, 0, 0, 0], autopct="%1.0f%%")
plt.title("Python leads, with a shadow")
plt.show()
print("shadow on")Output:
shadow onKey takeaways
-
explode holds one offset per slice; pull out one slice, not all
-
startangle=90 starts at twelve o'clock; counterclock=False runs clockwise
-
wedgeprops={"width": 0.4} turns a pie into a donut
-
Move crowded labels into a legend using the wedges pie returns
-
Next: Saving Figures
-
Previous: Pie Charts
Loading playground…
Check your understanding
Styling a Pie Chart Quiz
What does explode do?
1 / 10