Adding labels to pie chart wedges#

The new pie_label method adds a label to each wedge in a pie chart created with pie. It can take

  • a list of strings, similar to the existing labels parameter of pie

  • a format string similar to the existing autopct parameter of pie except that it uses the str.format method and it can handle absolute values as well as fractions/percentages

For more examples, see Labeling pie charts.

import matplotlib.pyplot as plt

data = [36, 24, 8, 12]
labels = ['spam', 'eggs', 'bacon', 'sausage']

fig, ax = plt.subplots()
pie = ax.pie(data)

ax.pie_label(pie, labels, distance=1.1)
ax.pie_label(pie, '{frac:.1%}', distance=0.7)
ax.pie_label(pie, '{absval:d}', distance=0.4)

(Source code, 2x.png, png)

A pie chart with three labels on each wedge, showing a food type, number, and fraction associated with the wedge.