Pie Charts in Python

There are a number of important types of plots that are used in descriptive statistics. A common plot that is frequently used in newspapers and magazines is the pie plot, where the size of a "wedge of pie" help the reader visualize the percentage of data falling in a particular category. Take, for example, the responses of children who were asked their favorite color of M and M candies.

Favorite Color
Red 40
Blue 30
Green 20
Brown 10

Note that the data in the table are percentages, summing to 100%.

First Pie Script

We'll now write a script that will produce a pie chart based on the data from the table above.

#! /usr/local/bin/python

from pylab import *

figure(1, figsize=(4,4))
axes([0.1, 0.1, 0.8, 0.8])

fracs=[40, 30, 20, 10]
pie(fracs)

savefig('../graphics/pie1')
show()

The first two lines should be familiar. The first line is a special type of comment which is used to locate the python interpreter. The #! must be located on the first line and in the first column of the source code. Otherwise, it will not be recognized. The next line from pylab import * will import all of the macros from the pylab library.

The next line figure(1, figsize=(4,4)) is used to create a new figure window. It is given an indentifying number (1 in this case) which can be used later in the program to bring this particular figure window to the forefront for plotting or viewing. You can have several figure opened at once, each with its own number, and the command figure(n) can be used to bring the nth figure window to the foreground. The current program makes no use of this design feature, but we mention it for future use.

The command axes([0.1,0.1,0.8,0.8]) is used to create an axes object in the figure window. The numbers are "normalized," with the first two coordinates (0.1 and 0.1) giving the position of the lower left-hand corner of the axes in the figure window. The second pair of coordinates (0.8 and 0.8) give the width and the height of the axes. As we shall soon see, this sizing of the axes "squares up" the axes and as a result the pie chart will appear circular instead of elliptical.

The list assignment frac=[40, 30, 20, 10] assigns percentages as delineated in the table above. They represent the percentages of children who favored red, blue, green, and brown colors. The pie(fracs) command produces the pie plot shown in Figure 1. The "wedges of pie" are shaded with default colors. Later in the activity we will show how to paint the "wedges of pie" with colors we choose.

A simple pie chart depicting the percentage of children who favor red, blue, green, and brown candies.

Figure 1. A simple pie chart representing the percentages of children favoring red, blue, green and brown candies.

There remain two additional commands that bear explanation. First, the command savefig('../graphics/pie1') saves the resulting figure. The default is to save the file as a PNG file. The string '../graphics/pie1' is used to save the pie image one directory up and into the graphics directory as pie1.png. This is known as "relative addressing." Finally, the command show() produces the plot on your computer screen.

Adding Labels

We will now adjust our script file to add labels to our "wedges of pie."

#! /usr/local/bin/python

from pylab import *

figure(2, figsize=(4,4))
axes([0.1, 0.1, 0.8, 0.8])

fracs=[40, 30, 20, 10]
mylabels=['Red', 'Blue', 'Green', 'Brown']
pie(fracs,labels=mylabels)

savefig('../graphics/pie2')

show()

There are only two new lines of consequence in this second script, namely:

mylabels=['Red', 'Blue', 'Green', 'Brown']
pie(fracs,labels=mylabels)

The first command stores a list containing the colors of the candies as strings in the variable mylabels. The second command draw the pie chart as before, but this time uses the list of colors stored in mylabels as labels. The result is the plot shown in Figure 2.

In this figures we've added labels

Figure 2. Here we add labels representing the candy colors selected by the children..

Choosing Colors

Let's coordinate colors with the color of the candies selected by the childresn. This is easily down by creating a list of colors in the same sequence order as our labels and percentages.

#! /usr/local/bin/python

from pylab import *

figure(3, figsize=(4,4))
axes([0.1, 0.1, 0.8, 0.8])

mycolors=['red', 'blue', 'green', 'brown']
mylabels=['Red', 'Blue', 'Green', 'Brown']
fracs=[40, 30, 20, 10]
pie(fracs,labels=mylabels,colors=mycolors)

savefig('../graphics/pie3')

show()

This code produces the pie chart shown in Figure 3.

Adding personal colors to the wedges of the pie chart.

Figure 3. Here we color each wedge to match the color of the candies selected by the children..