Skip to content

Pie Charts

Pie Charts

Pie charts are created using with Matplotlib's plt.pie() function. Decimal percentages or any list of floats or integers can be used to construct a pie chart. This first example pie chart describes the cost breakdown of building a microcontroller.

The cost breakdown for a manufactured item, like a microcontroller, can be divided into four cost categories: engineering (including design), manufacturing (including raw materials), sales (including marketing) and profit.

These cost categories applied to a $9.00 microcontroller:

  • Engineering $1.35
  • Manufacturing $3.60
  • Sales $2.25
  • Profit $1.80

Our pie chart will show the cost breakdown as different sized pieces. Matplotlib's plt.pie() function only requires one positional argument, a list or array of sizes. The sizes list or array passed to plt.pie() do not need to be normalized (add up to 100\%).

To construct the chart, import matplotlib and include %matplotlib inline if using a Jupyter notebook. Create a list of pie piece sizes and call the plt.pie() function.

We can add some labels to our pie chart with the keyword argument labels= included in the plt.pie() function call. The labels need to be a Python list of strings.

A title is added to the pie chart with plt.title(). This syntax is similar to including titles on line plots and bar charts.

It is essential to include the line plt.axis('equal') when building pie charts in Matplotlib. This line sets the x and y-axis scales as equal, which forces the pie chart into a circle shape. Without ``plt.axis('equal')```, the pie chart looks like an oval.

In [1]:
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

sizes = [1.35, 3.60, 2.25, 1.80] labels = 'Engineering', 'Manufacturing', 'Sales', 'Profit'

plt.pie(sizes, labels = labels)

plt.title('Cost breakdown of a $9 microcontroller') plt.axis('equal')

plt.show()

We can highlight the engineering cost by exploding out a piece of the pie chart. explode= is an example of a plt.pie() keyword argument. The table below shows a number of useful pie chart keyword arguments:

pie chart feature keyword argument Example
labels of pieces labels= plt.pie(sizes, labels=['sales','profit'])
show percentages autopct= plt.pie(sizes, autopct='%1.1f%%')
colors of pieces colors= plt.pie(sizes, colors=['r','g','b'])
explode out pieces explode= plt.pie(sizes, explode=(0.1, 0, 0))
starting angle of first piece startangle= plt.pie(sizes, startangle=90)
rotate labels with pieces rotatelabels= plt.pie(sizes, rotatelabels=True)
shadow behind pieces shadow= plt.pie(sizes, shadow=True)
Let's add the following features to the pie chart:
  • standard colors
  • Engineering piece exploded outwards
  • percentages auto-calculated and shown
  • shadow on each piece
  • Engineering piece start at zero degrees relative to the positive x-axis
    In [2]:
    import matplotlib.pyplot as plt
    %matplotlib inline

# Pie chart, where the slices will be ordered and plotted counter-clockwise: labels = 'Engineering', 'Manufacturing', 'Sales', 'Profit' sizes = [1.35, 3.60, 2.25, 1.80] explode = (0.1, 0, 0, 0) # "explode" the 1st slice, 'Engineering'

plt.pie(sizes, labels=labels, explode=explode, autopct='%1.1f%%', shadow=True, startangle=0)

plt.title('Cost breakdown of a $9 microcontroller')

plt.axis('equal') plt.show()