Say you have a list of country names and you want to highlight those countries on a map, shaded by the number of repitions of that country in the list. You might want to go from a list like this:
locations = ['Slovenia',
'Slovenia',
'Italy',
'Russian Federation',
'Serbia',
'United Kingdom',
'Germany',
'Norway',
'Peru',
'Poland',
'Moldova',
...
'Germany',
'France',
'United Kingdom']
To an image like this, from google charts:
The google charts API requires ISO 3166 country codes, pycountry can translate country names to ISO 3166 codes:
codes = [pycountry.countries.get(name=l).alpha2 for l in locations]
Counting the repitions in codes is easy:
code_count = defaultdict(int)
for code in codes:
code_count[code] += 1
The last thing to do is save the parameters and make the charts URL:
base = 'http://chart.apis.google.com/chart?'
params = {
'chco': 'FFFFFF,CCFFCC,88FF88,00FF00',
'chf': 'bg,s,EAF7FE',
'chs': '440x220',
'cht': 't',
'chtm': 'world'}
params['chd'] = 't:'+
",".join(
str(int((counts[k]/float(max(counts.values()))*100)))
for k in sorted(counts.keys()))
params['chld'] = "".join(sorted(counts.keys()))
URL = base + "&".join(
"=".join([k, params[k]) for k in params.keys())
After all that URL looks like:
http://chart.apis.google.com/chart?chd=t:60,20,20,100,40,100,60,20,20,20,20,20,20,100,20,60,60&chf=bg,s,EAF7FE&chco=FFFFFF,CCFFCC,88FF88,00FF00&chtm=world&chld=BEBYCZDEFRGBITMDNOPEPLPTRSRUSESIUA&chs=440x220&cht=t
Which is the link to the map shown above.