[Python Crash Course] DataVisual_GlobalData

import json

from pygal_maps_world.i18n import COUNTRIES  # i18n: internationalization / contains two-letter names

import pygal_maps_world.maps                 # figure maps

from pygal.style import RotateStyle          # style maps

from pygal.style import LightColorizedStyle  # lighten the style


# load data into list #
filename = 'population_data.json'
with open(filename) as f:
    pop_data = json.load(f)                    # json.load(): convert the format

# the standard counter code #
for country_code in sorted(COUNTRIES.keys()):  # sorted in the alphabetical order
    # print(country_code, COUNTRIES[country_code])
    pass


# extract the country code data #
def get_country_code(country_names):
    """ return the 2-digit country code for the given country """
    for codes, names in COUNTRIES.items():
        if names == country_names:
            return codes
    return None  # if found will not use the second return


# print the population in 2010 #
count = 0
cc_populations = {}  # a dictionary of population data
for pop_dict in pop_data:
    if pop_dict['Year'] == '2010':
        country_name = pop_dict['Country Name']
        country_pop = int(float(pop_dict['Value']))  # python cannot change decimal into integer directly
        # print(country_name + ':' + str(country_pop))
        code = get_country_code(country_name)
        if code:
            # print(code + ":" + str(country_pop))
            cc_populations[code] = country_pop
        else:
            # print('ERROR-' + country_name)              # reason: 1.regions and economic groups 2.different system
            count = count + 1
print("The ERROR number:", str(count))

# build a world map #
wm = pygal_maps_world.maps.World()
wm.title = 'North, Central, and South America'
wm.add('North America', ['ca', 'mx', 'us'])                # add('area title', [countries])
wm.add('Central America', ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv'])
wm.add('South America', ['ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf', 'gy', 'pe', 'py', 'sr', 'uy', 've'])
wm.render_to_file('americas.svg')

# plot numerical data on a world map #
wm = pygal_maps_world.maps.World()
wm.title = 'Population of Countries in North America'
wm.add('North America', {'ca': 34126000, 'us': 309349000, 'mx': 113423000})
wm.render_to_file('PopAmerica.svg')

# plot population data on a world map #
wm = pygal_maps_world.maps.World()
wm.title = 'Population around the world in 2010'
wm.add('2010', cc_populations)
wm.render_to_file('PopWorld.svg')

 

# group the countries into 3 groups and plot #
cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {}
for cc, pop in cc_populations.items():
    if pop < 10000000:
        cc_pops_1[cc] = pop
    elif pop < 1000000000:
        cc_pops_2[cc] = pop
    else:
        cc_pops_3[cc] = pop
print("the number in different groups:", len(cc_pops_1), len(cc_pops_2), len(cc_pops_3))

 

# an RGB color in hex format
wm_style = RotateStyle('#336699', base_style=LightColorizedStyle)
wm = pygal_maps_world.maps.World(style=wm_style)

# wm = pygal_maps_world.maps.World()
wm.title = 'Population around the world in 2010, by group'
wm.add('0-10m', cc_pops_1)
wm.add('10m-1bn', cc_pops_2)
wm.add('>1bn', cc_pops_3)
wm.render_to_file('PopGroup_blue_light.svg')

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值