In [8]:
# Make a pie chart
# This script is written by Vamei, http://www.cnblogs.com/vamei
# you may freely use it.
import os
import matplotlib.pyplot as plt

# quants: GDP
# labels: country name
labels   = []
quants   = []

# Read data
f = open("gdp.txt")
for line in f:
    info = line.split()
    print(info)
    
    labels.append(info[0])
    quants.append(float(info[1]))
f.close()


['USA', '15094025']
['China', '11299967']
['India', '4457784']
['Japan', '4440376']
['Germany', '3099080']
['Russia', '2383402']
['Brazil', '2293954']
['UK', '2260803']
['France', '2217900']
['Italy', '1846950']

In [9]:
# For China, make the piece explode a bit
def explode(label, target='China'):
    if label == target: return 0.1
    else: return 0

expl = map(explode,labels)

# make a square figure
plt.figure(1, figsize=(6,6))

# Colors used. Recycle if not enough.
colors  = ["pink","coral","yellow","orange"]

In [10]:
# Pie Plot
# autopct: format of "percent" string;
plt.pie(quants, explode=expl, colors=colors, labels=labels, autopct='%1.1f%%',pctdistance=0.8, shadow=True)
plt.title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5})
plt.show()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-9ec662c26f67> in <module>()
      1 # Pie Plot
      2 # autopct: format of "percent" string;
----> 3 plt.pie(quants, explode=expl, colors=colors, labels=labels, autopct='%1.1f%%',pctdistance=0.8, shadow=True)
      4 plt.title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5})
      5 plt.show()

/media/supermap/Application/OpenAI/anaconda3/envs/tensor/lib/python3.5/site-packages/matplotlib/pyplot.py in pie(x, explode, labels, colors, autopct, pctdistance, shadow, labeldistance, startangle, radius, counterclock, wedgeprops, textprops, center, frame, hold, data)
   3135                      radius=radius, counterclock=counterclock,
   3136                      wedgeprops=wedgeprops, textprops=textprops, center=center,
-> 3137                      frame=frame, data=data)
   3138     finally:
   3139         ax.hold(washold)

/media/supermap/Application/OpenAI/anaconda3/envs/tensor/lib/python3.5/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1810                     warnings.warn(msg % (label_namer, func.__name__),
   1811                                   RuntimeWarning, stacklevel=2)
-> 1812             return func(ax, *args, **kwargs)
   1813         pre_doc = inner.__doc__
   1814         if pre_doc is None:

/media/supermap/Application/OpenAI/anaconda3/envs/tensor/lib/python3.5/site-packages/matplotlib/axes/_axes.py in pie(self, x, explode, labels, colors, autopct, pctdistance, shadow, labeldistance, startangle, radius, counterclock, wedgeprops, textprops, center, frame)
   2558         if len(x) != len(labels):
   2559             raise ValueError("'label' must be of length 'x'")
-> 2560         if len(x) != len(explode):
   2561             raise ValueError("'explode' must be of length 'x'")
   2562         if colors is None:

TypeError: object of type 'map' has no len()

In [ ]: