下载数据(matplotlib可视化16章-2)

1、加载json数据

import json
from country_codes import get_coutry_code

filename = 'population_data.json'

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

#打印各个国家2010年的人口数量
for pop_dict in pop_data:
    if pop_dict['Year'] == '2010':
        country_name = pop_dict['Country Name']
        population = int(float(pop_dict['Value']))
        print(country_name+" "+str(population))

        code = get_coutry_code(country_name)
        if code:
            print(code + ':' + str(population))
        else:
            print('ERROR-'+country_name)

2、画世界地图

import pygal_maps_world.maps

wm = pygal_maps_world.maps.World()
wm.title = 'North,Central,and South America'

wm.add('North America',['ca','mx','us'])
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.add('North America',{'ca':34126000,'ux':30934900,'mx':113423000})
wm.render_to_file('na_americas.svg')

 

posted @ 2022-11-21 15:26  水牛打老鼠  阅读(56)  评论(0)    收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css