Pie Charts

Pie charts are as simple to use as bar charts. The function that implements pie charts is pie(x), where x holds the values to be charted.

Example: GDP, Pie Chart

Listing 6-10 presents a script to generate a pie chart, shown in Figure 6-11, again making use of the function read_world_data().

Listing 6-10. Plotting a GDP Pie Chart

# a script to plot GDP pie chart from pylab import *

# initialize variables, N is the number of countries N = 10

execfile('read_world_data.py') gdp, tags = read_world_data(N)

# plot the bar chart pie(gdp, labels=tags, shadow=True) title('GDP rank, data from CIA World Factbook')

GDP rank, data from CIA World Factbook

Pie Chart Gdp Worldwide 2010

Germany United Kingdom

Figure 6-11. GDP pie chart, N largest economies

Germany United Kingdom

Figure 6-11. GDP pie chart, N largest economies

■ Note I've decided to use the variable tags instead of labels so that the call to pie() would be a little less confusing. Had I stuck with the original name, labels, the call to pie would've been pie(gdp, labels=labels, shadow=True), which still would've worked, but would seem a bit confusing in my opinion.

Was this article helpful?

0 -1

Post a comment