process json in pygal

import json
from random_walk import get_country_code
from pygal.maps.world import World
from pygal.style import RotateStyle
from pygal.style import LightColorizedStyle
#from pygal.style import RotateStyle as RS,LightColorStyle as LCS

filename = 'population_data.json'
with open(filename) as f:
    pop_data = json.load(f)

cc_population={}

for pop_dict in pop_data:
    if pop_dict['Year'] == '2010':
        country_name = pop_dict['Country Name']
        population = int(float(pop_dict['Value']))
        code = get_country_code(country_name)
        if code:
            cc_population[code]=population

cc_1,cc_2,cc_3={},{},{}
for cc,pop in cc_population.items():
    if pop <10000000:
        cc_1[cc]=pop
    elif pop <1000000000:
        cc_2[cc]=pop
    else:
        cc_3[cc]=pop

print(len(cc_1),len(cc_2),len(cc_3))


#wm=World()

#set the basic color
#wm_style = RotateStyle('#336699')

#brighten the backgroudcolor
#wm_style = LightColorizedStyle

wm_style = RotateStyle('#336699',base_style=LightColorizedStyle)


wm=World(style=wm_style)

wm.title = "World population in 2010,by Country"
#wm.add('2010',cc_population)

#group the data
wm.add('0-10m',cc_1)
wm.add('10m-1bn',cc_2)
wm.add('>1bn',cc_3)

wm.render_to_file('World_population.svg')

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章