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
piea format string similar to the existing autopct parameter of
pieexcept that it uses thestr.formatmethod 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)