pie() (Matplotlib)

ax.pie() draws a pie chart, dividing a circle into wedges whose angles are proportional to each value's share of the total. It shows how parts make up a whole.

Matplotlib

Pass a sequence of values and pie sizes each wedge by its fraction of the sum. Common arguments include labels for the slice names, autopct='%1.1f%%' to print each slice's percentage, explode to pull a slice out for emphasis, and startangle to rotate the whole chart.

Pie charts are best for a small number of categories that sum to a meaningful whole. With many slices, or when precise comparison matters, a bar chart is easier to read because humans judge lengths better than angles — a frequent data-visualization caution.

pie returns the wedge patches (and optionally the text labels), and you usually set the Axes aspect to equal so the pie is a circle rather than an ellipse.

Syntax

ax.pie(values, labels=None, autopct=None, explode=None)

Examples

Python — One wedge per value.

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
wedges, texts = ax.pie([1, 2, 3])
print(len(wedges))

Output:

3

Learn it in a lesson

See also

Back to the Dictionary