python世界人口地圖

sdk版本:
pygal 2.4.0
pygal-maps-world 1.0.2

import json
import pygal.maps.world
from pygal_maps_world import i18n

def get_country_code(country_name):
for code,name in i18n.COUNTRIES.items():
if name == country_name:
return code

filename = “population_data.json”
with open(filename,“r”) as f:
pop_data = json.load(f)

c_populations = {}
for pop_dict in pop_data:
if pop_dict[“Year”] == “2010”:
country_name = pop_dict[“Country Name”]
populition = int(float(pop_dict[“Value”]))
code = get_country_code(country_name)
if code:
c_populations[code] = populition

cc_pops_1,cc_pops_2,cc_pops_3 = {},{},{}
for cc,pop in c_populations.items():
if pop < 10000000:
cc_pops_1[cc] = pop
elif pop < 1000000000:
cc_pops_2[cc] = pop
else:
cc_pops_3[cc] = pop

wm = pygal.maps.world.World() # 生成世界地圖實例
wm.title = ‘World Population in 2010, by Country’ # 設置標題
wm.add(‘1-10m’,cc_pops_1) # 添加0——1千萬的國家和人口
wm.add(‘10m-1bn’,cc_pops_2) # 添加1千萬——10億的國家和人口
wm.add(’>bn’,cc_pops_3) # 添加10億以上的國家和人口
wm.render_to_file(‘world_population.svg’)

用瀏覽器打開world_population.svg文件即可
在這裏插入圖片描述

使用數據:
鏈接:https://pan.baidu.com/s/1TJkaBF_NvjYzjQGAafj_gA 提取碼:6JP4

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