-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path4_compare_indicators_starter.py
72 lines (46 loc) · 1.42 KB
/
4_compare_indicators_starter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import csv
from collections import OrderedDict
import matplotlib.pyplot as plt
import numpy
def main():
csvfile = open('data/fertility.csv', 'rU')
reader = csv.DictReader(csvfile)
# add more here
compareIndicators()
return
# Fill in here
def compareIndicators():
return
def compareCountries(reader, country_name1, country_name2 = None, country_name3 = None):
for row in reader:
if row['Country Name'] == country_name1:
data1 = extractData(row)
if row['Country Name'] == country_name2:
data2 = extractData(row)
if row['Country Name'] == country_name3:
data3 = extractData(row)
fig = plt.figure()
ax = fig.add_subplot(111)
index = numpy.arange(len(data1))
width = 0.35
series1 = ax.bar(index, data1.values(), width, color="r")
series2 = ax.bar(index+width, data2.values(), width, color="y")
series3 = ax.bar(index+2*width, data3.values(), width)
ax.legend( (series1[0], series2[0], series3[0]), (country_name1, country_name2, country_name3))
ax.set_xticklabels(range(1960, 2013), rotation="vertical")
ax.set_xticks(index+width)
plt.show()
return
## Returns data as years-to-values only, stripping out country name, acronym, etc
def extractData(row):
data = {}
for key, value in row.iteritems():
if not key.isdigit():
continue
if int(key) not in range(1960, 2013):
continue
if value is '':
value = 0
data[int(key)] = float(value)
return OrderedDict(sorted(data.items()))
main()