2019-01-14

16.2.7绘制完整的世界人口地图

worl_population.py

import json#导入模块json,便于正确的加载json格式中的数据
import pygal#导入模块pygal,便于正确加载图标
from country_code import get_country_code#调用程序country_code中方法get_country_code
filename = 'population_data.json'
with open(filename) as f:
    pop_data=json.load(f)#将数据储存到pop data中
for pop_dict in pop_data:#遍历从pop data中的元素,每一个元素其实都是一个字典,包含四个键值对,将每个字典传给pop dict储存
    if pop_dict['Year']=='2010':#检查判断键为Year对应的值是2010时,
        country_name = pop_dict['Country Name']#把字典中Country Name的值传给country_name
        population = int(float(pop_dict['Value']))#把字典中Value的值转化为浮点数在转化整数型传给population,
        print(country_name+":"+str(population))#根据传输的值,打印出来,str是字符串格式

cc_population = {}#创建一个空字典,用于下边数据的储存
for pop_dict in pop_data:
    if pop_dict['Year']=='2010':
        country = pop_dict['Country Name']
        population = int(float(pop_dict['Value']))
        code = get_country_code(country)
        if code:
            cc_population[code]=population
wm = pygal.maps.world.World()#创建实例maps.world.world
wm.title = 'World Population in 2010 ,by county'#设置该实例的标题为'World Population in 2010 ,by county'
wm.add('2010',cc_population)#调用add(),向它传递国别码、人口数量组成的字典
wm.render_to_file('World_Population.svg')#生成文件名称为World_population.svg的文件,使用浏览器打开即可访问

输出显示

ad
ae
af
Arab World:357868000
Caribbean small states:6880000
East Asia & Pacific (all income levels):2201536674
East Asia & Pacific (developing only):1961558757
Euro area:331766000
--snip--
Zambia:12927000
Zimbabwe:12571000

16.2.8根据人口数量将国家分组

worl_population.py

import json#导入模块json,便于正确的加载json格式中的数据
import pygal#导入模块pygal,便于正确加载图标
from country_code import get_country_code#调用程序country_code中方法get_country_code
filename = 'population_data.json'
with open(filename) as f:
    pop_data=json.load(f)#将数据储存到pop data中
for pop_dict in pop_data:#遍历从pop data中的元素,每一个元素其实都是一个字典,包含四个键值对,将每个字典传给pop dict储存
    if pop_dict['Year']=='2010':#检查判断键为Year对应的值是2010时,
        country_name = pop_dict['Country Name']#把字典中Country Name的值传给country_name
        population = int(float(pop_dict['Value']))#把字典中Value的值转化为浮点数在转化整数型传给population,
        #print(country_name+":"+str(population))#根据传输的值,打印出来,str是字符串格式

cc_population = {}#创建一个空字典,用于下边数据的储存
for pop_dict in pop_data:
    if pop_dict['Year']=='2010':
        country = pop_dict['Country Name']
        population = int(float(pop_dict['Value']))
        code = get_country_code(country)
        if code:
            cc_population[code]=population
            
            
cc_pops_1,cc_pops_2,cc_pops_3 = {},{},{}#创建三个空字典,用于储存分组的国家数据
for cc,pop in cc_population.items():#字典调用方法iitems,可遍历处键值数组
    if pop<10000000:
        cc_pops_1[cc]=pop
    elif pop< 1000000000:
        cc_pops_2[cc]=pop
    else:
        cc_pops_3[cc]=pop
print(len(cc_pops_1),len(cc_pops_2),len(cc_pops_3))#统计每一组有多少国家
        
wm = pygal.maps.world.World()#创建实例maps.world.world
wm.title = 'World Population in 2010 ,by county'#设置该实例的标题为'World Population in 2010 ,by county'
wm.add('0-10m',cc_pops_1)#调用add(),向它传递国别码、人口数量组成的字典
wm.add('10-1bm',cc_pops_2)
wm.add('>1bm',cc_pops_3)
wm.render_to_file('World_Population2.svg')#生成文件名称为World_population.svg的文件,使用浏览器打开即可访问

输出结果

85 69 2

16.2.9使用pygal设置世界地图的样式

world_population

import json#导入模块json,便于正确的加载json格式中的数据
import pygal#导入模块pygal,便于正确加载图标
from country_code import get_country_code#调用程序country_code中方法get_country_code
from pygal.style import RotateStyle
filename = 'population_data.json'
with open(filename) as f:
    pop_data=json.load(f)#将数据储存到pop data中
for pop_dict in pop_data:#遍历从pop data中的元素,每一个元素其实都是一个字典,包含四个键值对,将每个字典传给pop dict储存
    if pop_dict['Year']=='2010':#检查判断键为Year对应的值是2010时,
        country_name = pop_dict['Country Name']#把字典中Country Name的值传给country_name
        population = int(float(pop_dict['Value']))#把字典中Value的值转化为浮点数在转化整数型传给population,
        #print(country_name+":"+str(population))#根据传输的值,打印出来,str是字符串格式

cc_population = {}#创建一个空字典,用于下边数据的储存
for pop_dict in pop_data:
    if pop_dict['Year']=='2010':
        country = pop_dict['Country Name']
        population = int(float(pop_dict['Value']))
        code = get_country_code(country)
        if code:
            cc_population[code]=population
            
            
cc_pops_1,cc_pops_2,cc_pops_3 = {},{},{}#创建三个空字典,用于储存分组的国家数据
for cc,pop in cc_population.items():#字典调用方法iitems,可遍历处键值数组
    if pop<10000000:
        cc_pops_1[cc]=pop
    elif pop< 1000000000:
        cc_pops_2[cc]=pop
    else:
        cc_pops_3[cc]=pop
print(len(cc_pops_1),len(cc_pops_2),len(cc_pops_3))#统计每一组有多少国家
   
wm = pygal.maps.world.World()#创建实例maps.world.world
wm.title = 'World Population in 2010 ,by county'#设置该实例的标题为'World Population in 2010 ,by county'
wm_style = RotateStyle('#336699') 
wm=pygal.maps.world.World(style=wm_style)
wm.add('0-10m',cc_pops_1)#调用add(),向它传递国别码、人口数量组成的字典
wm.add('10-1bm',cc_pops_2)
wm.add('>1bm',cc_pops_3)
wm.render_to_file('World_Population3.svg')#生成文件名称为World_population.svg的文件,使用浏览器打开即可访问

输出

85 69 2

你可能感兴趣的:(2019-01-14)