In [62]:
%matplotlib inline
import string
import urllib2
import csv
import numpy as np
import matplotlib.pyplot as plt
def convert_int(n):
if not n or n == '0':
return 0
if n.isdigit():
return int(n)
return n
url = "http://www.mosaic-web.org/go/datasets/Gapminder/CellPhoneTotal.csv"
fd = urllib2.urlopen(url)
datareader = csv.reader(fd)
raw_data = []
for i, row in enumerate(datareader):
raw_data.append(row)
raw_data[:10]
Out[62]:
[['Mobile cellular subscriptions, total number',
'1965',
'1966',
'1967',
'1968',
'1969',
'1970',
'1971',
'1972',
'1973',
'1974',
'1975',
'1976',
'1977',
'1978',
'1979',
'1980',
'1981',
'1982',
'1983',
'1984',
'1985',
'1986',
'1987',
'1988',
'1989',
'1990',
'1991',
'1992',
'1993',
'1994',
'1995',
'1996',
'1997',
'1998',
'1999',
'2000',
'2001',
'2002',
'2003',
'2004',
'2005',
'2006',
'2007',
'2008',
'2009',
'2010',
'2011'],
['Abkhazia',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
''],
['Afghanistan',
'0',
'',
'',
'',
'',
'0',
'',
'',
'',
'',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'25000',
'200000',
'600000',
'1200000',
'2520366',
'4668096',
'7898909',
'10500000',
'13000000',
'17558265'],
['Akrotiri and Dhekelia',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
''],
['Albania',
'0',
'',
'',
'',
'',
'0',
'',
'',
'',
'',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'2300',
'3300',
'5600',
'11008',
'29791',
'392650',
'851000',
'1100000',
'1259590',
'1530244',
'1909885',
'2322436',
'1859632',
'2463741',
'2692372',
'3100000'],
['Algeria',
'0',
'',
'',
'',
'',
'0',
'',
'',
'',
'',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'470',
'4781',
'4781',
'4781',
'1348',
'4691',
'11700',
'17400',
'18000',
'72000',
'86000',
'100000',
'450244',
'1446927',
'4882414',
'13661355',
'20997954',
'27562721',
'27031472',
'32729824',
'32780165',
'35615926'],
['American Samoa',
'0',
'',
'',
'',
'',
'0',
'',
'',
'',
'',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'',
'',
'',
'0',
'0',
'700',
'900',
'1200',
'1250',
'1300',
'1400',
'1500',
'1800',
'1992',
'2156',
'2036',
'2100',
'2250',
'',
'',
'',
'',
'',
'',
''],
['Andorra',
'0',
'',
'',
'',
'',
'0',
'',
'',
'',
'',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'770',
'780',
'784',
'2825',
'5488',
'8618',
'14117',
'20600',
'23543',
'29429',
'32790',
'51893',
'58366',
'64560',
'69004',
'63503',
'64202',
'64549',
'65495',
'65044'],
['Angola',
'0',
'',
'',
'',
'',
'0',
'',
'',
'',
'',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'1100',
'1824',
'1994',
'3298',
'7052',
'9820',
'24000',
'25806',
'75000',
'140000',
'350000',
'740000',
'1611118',
'3054620',
'4961536',
'6773356',
'8109421',
'8909200',
'9491000'],
['Anguilla',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'']]
In [63]:
data = []
for row in raw_data[1:]:
data.append([convert_int(x) for x in row[1:]])
countries = [x[0] for x in raw_data[1:]]
print countries
years = [int(x) for x in raw_data[0][1:]]
print years
adata = np.array(data)
print adata
['Abkhazia', 'Afghanistan', 'Akrotiri and Dhekelia', 'Albania', 'Algeria', 'American Samoa', 'Andorra', 'Angola', 'Anguilla', 'Antigua and Barbuda', 'Argentina', 'Armenia', 'Aruba', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 'Benin', 'Bermuda', 'Bhutan', 'Bolivia', 'Bosnia and Herzegovina', 'Botswana', 'Brazil', 'British Virgin Islands', 'Brunei', 'Bulgaria', 'Burkina Faso', 'Burundi', 'Cambodia', 'Cameroon', 'Canada', 'Cape Verde', 'Cayman Islands', 'Central African Republic', 'Chad', 'Channel Islands', 'Chile', 'China', 'Christmas Island', 'Cocos Island', 'Colombia', 'Comoros', 'Congo, Dem. Rep.', 'Congo, Rep.', 'Cook Is', 'Costa Rica', "Cote d'Ivoire", 'Croatia', 'Cuba', 'Cyprus', 'Czech Republic', 'Czechoslovakia', 'Denmark', 'Djibouti', 'Dominica', 'Dominican Republic', 'East Germany', 'Ecuador', 'Egypt', 'El Salvador', 'Equatorial Guinea', 'Eritrea', 'Eritrea and Ethiopia', 'Estonia', 'Ethiopia', 'Faeroe Islands', 'Falkland Is (Malvinas)', 'Fiji', 'Finland', 'France', 'French Guiana', 'French Polynesia', 'Gabon', 'Gambia', 'Georgia', 'Germany', 'Ghana', 'Gibraltar', 'Greece', 'Greenland', 'Grenada', 'Guadeloupe', 'Guam', 'Guatemala', 'Guernsey', 'Guinea', 'Guinea-Bissau', 'Guyana', 'Haiti', 'Holy See', 'Honduras', 'Hong Kong, China', 'Hungary', 'Iceland', 'India', 'Indonesia', 'Iran', 'Iraq', 'Ireland', 'Isle of Man', 'Israel', 'Italy', 'Jamaica', 'Japan', 'Jersey', 'Jordan', 'Kazakhstan', 'Kenya', 'Kiribati', 'North Korea', 'South Korea', 'United Korea (former)\r\n', 'Kosovo', 'Kuwait', 'Kyrgyz Republic', 'Lao', 'Latvia', 'Lebanon', 'Lesotho', 'Liberia', 'Libya', 'Liechtenstein', 'Lithuania', 'Luxembourg', 'Macao, China', 'Macedonia, FYR', 'Madagascar', 'Malawi', 'Malaysia', 'Maldives', 'Mali', 'Malta', 'Marshall Islands', 'Martinique', 'Mauritania', 'Mauritius', 'Mayotte', 'Mexico', 'Micronesia, Fed. Sts.', 'Moldova', 'Monaco', 'Mongolia', 'Montenegro', 'Montserrat', 'Morocco', 'Mozambique', 'Myanmar', 'Namibia', 'Nauru', 'Nepal', 'Netherlands', 'Netherlands Antilles', 'New Caledonia', 'New Zealand', 'Ngorno-Karabakh', 'Nicaragua', 'Niger', 'Nigeria', 'Niue', 'Norfolk Island', 'Northern Cyprus', 'Northern Mariana Islands', 'Norway', 'Oman', 'Pakistan', 'Palau', 'Panama', 'Papua New Guinea', 'Paraguay', 'Peru', 'Philippines', 'Pitcairn', 'Poland', 'Portugal', 'Puerto Rico', 'Qatar', 'Reunion', 'Romania', 'Russia', 'Rwanda', 'St. Barth\xc3\xa9lemy', 'St. Helena', 'St. Kitts and Nevis', 'St. Lucia', 'St. Martin', 'St. Vincent and the Grenadines', 'St.-Pierre-et-Miquelon', 'Samoa', 'San Marino', 'Sao Tome and Principe', 'Saudi Arabia', 'Senegal', 'Serbia', 'Serbia and Montenegro', 'Serbia excluding Kosovo', 'Seychelles', 'Sierra Leone', 'Singapore', 'Slovak Republic', 'Slovenia', 'Solomon Islands', 'Somalia', 'Somaliland', 'South Africa', 'South Ossetia', 'Spain', 'Sri Lanka', 'Sudan', 'Suriname', 'Svalbard', 'Swaziland', 'Sweden', 'Switzerland', 'Syria', 'Taiwan', 'Tajikistan', 'Tanzania', 'Thailand', 'Timor-Leste', 'Togo', 'Tokelau', 'Tonga', 'Transnistria', 'Trinidad and Tobago', 'Tunisia', 'Turkey', 'Turkmenistan', 'Turks and Caicos Islands', 'Tuvalu', 'Uganda', 'Ukraine', 'United Arab Emirates', 'United Kingdom', 'United States', 'Uruguay', 'USSR', 'Uzbekistan', 'Wallis et Futuna', 'Vanuatu', 'Venezuela', 'West Bank and Gaza', 'West Germany', 'Western Sahara', 'Vietnam', 'Virgin Islands (U.S.)', 'North Yemen (former)', 'South Yemen (former)', 'Yemen', 'Yugoslavia', 'Zambia', 'Zimbabwe', '\xc3\x85land', 'South Sudan', 'Christian', 'Coastline', 'Cura\xc3\xa7ao', 'Sint Maarten (Dutch part)', 'St. Martin (French part)', 'Antarctica', 'Virgin Islands, British', 'Hawaiian Trade Zone', 'U.S. Pacific Islands', 'Wake Island', 'Bonaire', 'Sark', 'Chinese Taipei', 'Saint Eustatius', 'Saba']
[1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011]
[[ 0 0 0 ..., 0 0 0]
[ 0 0 0 ..., 10500000 13000000 17558265]
[ 0 0 0 ..., 0 0 0]
...,
[ 0 0 0 ..., 0 0 0]
[ 0 0 0 ..., 0 0 0]
[ 0 0 0 ..., 0 0 0]]
In [64]:
plt.figure(figsize=(12, 8), dpi=50)
for i, country in enumerate(countries):
plt.plot(years, adata[i])
#plt.yscale('log')
plt.xlim((1980, 2012))
Out[64]:
(1980, 2012)
In [ ]:
Content source: davidvilla/python-course
Similar notebooks: