Labeling pie charts#

This example illustrates some features of the pie_label method, which adds labels to an existing pie chart created with pie.

The simplest option is to provide a list of strings to label each slice of the pie.

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)
pie label

If you want the labels outside the pie, set a distance greater than 1. This is the distance from the center of the pie as a fraction of its radius.

pie label

You can also rotate the labels so they are oriented away from the pie center.

pie label

Instead of explicit labels, pass a format string to label slices with their values...

fig, ax = plt.subplots()
pie = ax.pie(data)
ax.pie_label(pie, '{absval:.1f}')
pie label

...or with their percentages...

fig, ax = plt.subplots()
pie = ax.pie(data)
ax.pie_label(pie, '{frac:.1%}')
pie label

...or both.

fig, ax = plt.subplots()
pie = ax.pie(data)
ax.pie_label(pie, '{absval:d}\n{frac:.1%}')
pie label

Font styling can be configured by passing a dictionary to the textprops parameter.

fig, ax = plt.subplots()
pie = ax.pie(data)
ax.pie_label(pie, labels, textprops={'fontsize': 'large', 'color': 'white'})
pie label

pie_label can be called repeatedly to add multiple sets of labels.

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)

plt.show()
pie label

References

The use of the following functions, methods, classes and modules is shown in this example:

Tags: plot-type: pie level: beginner

Gallery generated by Sphinx-Gallery