Note

Go to the endto download the full example code.

Labeling pie charts#

This example illustrates some features of thepie_labelmethod, which adds labels to an existing pie chart created withpie.

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

importmatplotlib.pyplotaspltdata=[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 adistance 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...

pie label

...or with their percentages...

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 thetextprops 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 setsof 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 shownin this example:

Tags:plot-type: pielevel: beginner

Total running time of the script: (0 minutes 2.507 seconds)

Gallery generated by Sphinx-Gallery