(一) 制作世界人口地图:JSON格式
下载JSON格式的人口数据,并使用json模块来处理它,Pygal提供一个适合初学者使用的地图创建工具,使用它来对人口数据进行可视化,用来探索全球人口的分布情况。
1.下载世界人口数据地址:https://ehmatthes.github.io/pcc/
2.安装模块:pip install pygal_maps_world
3.将population_data.json拖到项目中,打开文件。
[
{
"Country Name": "Arab World",
"Country Code": "ARB",
"Year": "1960",
"Value": "96388069"
}
--snip--
]
(二)处理JSON文本的具体步骤
1 获取国家和人口数据,.创建world_population.py
import json
#1.将数据加载到一个列表中
filename='population_data.json'
with open(filename)as f:
pop_data=json.load(f)
#2.打印每个国家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))
结果演示:
Arab World:357868000
Caribbean small states:6880000
East Asia & Pacific (all income levels):2201536674
---snip---
Zambia:12927000
Zimbabwe:12571000
- 获取两个字母的国别码,创建countries.py
制作地图前,还需要解决数据存储在的最后一个问题。Pygal中的地图制作工具要求数据为特定格式,用国别码表示国家,用数字表示人口数量。处理地理政治数据时,经常需要标准化国别码集。population_data.json中包含的是三只字母的国别码,但Pygal使用两个字母的国别码,我们需要想办法,根据国家名获取两个字母的国别码。安装pip install pygal_maps_world即可!
#1.安装模块:pip install pygal_maps_world
from pygal_maps_world.i18n import COUNTRIES
for country_code in sorted(COUNTRIES.keys()):
print(country_code, COUNTRIES[country_code])
结果演示:
ad Andorra
ae United Arab Emirates
af Afghanistan
---snip---
za South Africa
zm Zambia
zw Zimbabwe
3.为了获取国别码,我们编写一个函数获取,创建country_codes.py
from pygal_maps_world.i18n import COUNTRIES
def get_country_code(country_name):
# 根据指定的国家,返回Pygal使用的两个字母的国别码
for code, name in COUNTRIES.items():
if name == country_name:
return code
# 如果没有找到指定的国家,就返回None
return None
print(get_country_code('Andorra'))
print(get_country_code('United Arab Emirates'))
print(get_country_code('Afghanistan'))
结果演示:
ad
ae
af
4.制作世界地图,创建americas.py
有了国别码后,制作世界地图非常简单。Pygal提供了一个图表类型的Worldmap,帮你制作呈现各国数据的世界地图。
# 绘制世界地图
import pygal
wm = pygal.maps.world.World()
wm.title = '北美 中部 南美'
wm.add('北美', ['ca', 'mx', 'us'])
wm.add('中部', ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv'])
wm.add('南美', ['ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf',
'gy', 'pe', 'py', 'sr', 'uy', 've'])
wm.render_to_file('americas.svg')
结果演示:
5.在世界地图上呈现数字数据(显示三个北美国家的人口数量),创建na_population.py
#显示北美三国2010年人口总数
import pygal
wm = pygal.maps.world.World()
wm.title="2010年北美三国人口总数"
wm.add('北美',{'ca':34126000,'us':309349000,'mx':113423000})
wm.render_to_file('na_population.svg')
结果演示:
6.绘制完整的世界人口地图
要呈现其他国家人口数量,需要将前面处理的数据转换为Pygal要求的字典格式,键为两个字母的国别码,值为人口数量。
import json
import pygal
from country_code import get_country_code
filename='population_data.json'
with open(filename) as file:
pop_data=json.load(file)
cc_populations={}
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_country_code(country_name)
if code:
cc_populations[code]=population
wm = pygal.maps.world.World()
wm.title = "2010年各国人口总数"
wm.add('2010', cc_populations)
wm.render_to_file("world_population.svg")
结果演示:
7.根据人口数量将国家分组
中国和印度的人口比其他国家多的多,但在当前地图中,它们的颜色和其他国家差别较小,中国和印度人口都超过了10亿,接下来人口最多的国家是美国大约3亿。下面将所有的国家作为一个编组,根据人口数量分为三组——少于1000万的、介于1000万和10亿的以及10亿以上的。
import json
import pygal
from country_code import get_country_code
filename='population_data.json'
with open(filename) as file:
pop_data=json.load(file)
cc_populations={}
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_country_code(country_name)
if code:
cc_populations[code]=population
#1.根据人口数量将所有国家分为三组
cc_pops_1,cc_pops_2,cc_pops_3={},{},{}
for cc,pop in cc_populations.items():
if pop<10000000:
cc_pops_1[cc]=pop
if pop<1000000000:
cc_pops_2[cc]=pop
else:
cc_pops_3[cc]=pop
#2.打印每个分组包含多少个国家
print(len(cc_pops_1),len(cc_pops_2),len(cc_pops_3))
wm = pygal.maps.world.World()
wm.title = "2010年各国人口总数"
wm.add('0-1000万',cc_pops_1)
wm.add('1000万-10亿',cc_pops_2)
wm.add('大于10亿',cc_pops_3)
wm.render_to_file("world_population.svg")
结果演示:
8.使用Pygal设置世界地图的样式
在这个地图中,根据人口将国家分组虽然很有效,但默认的颜色设置非常难看。例如,在这里Pygal选择鲜艳的粉色和绿色基色,下面使用Pygal样式设置指令调整颜色。
import json
import pygal
#1.导入设置颜色的模块
from pygal.style import RotateStyle
from country_code import get_country_code
filename='population_data.json'
with open(filename) as file:
pop_data=json.load(file)
cc_populations={}
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_country_code(country_name)
if code:
cc_populations[code]=population
#2.根据人口数量将所有国家分为三组
cc_pops_1,cc_pops_2,cc_pops_3={},{},{}
for cc,pop in cc_populations.items():
if pop<10000000:
cc_pops_1[cc]=pop
if pop<1000000000:
cc_pops_2[cc]=pop
else:
cc_pops_3[cc]=pop
#3.打印每个分组包含多少个国家
print(len(cc_pops_1),len(cc_pops_2),len(cc_pops_3))
#4.设置颜色参数
wm_style=RotateStyle('#336699')
wm = pygal.maps.world.World(style=wm_style)
wm.title = "2010年各国人口总数"
wm.add('0-1000万',cc_pops_1)
wm.add('1000万-10亿',cc_pops_2)
wm.add('大于10亿',cc_pops_3)
wm.render_to_file("world_population.svg")
结果演示:
9.加亮颜色主题
Pygal默认使用较暗颜色主题,为了渲染效果,我们使用LightColorizedStyle加亮地图的颜色。这个类修改整个图表,包括背景色、标签及各个国家的颜色。
import json
import pygal
#1.导入设置主题的模块的类
from pygal.style import LightColorizedStyle
from country_code import get_country_code
filename='population_data.json'
with open(filename) as file:
pop_data=json.load(file)
cc_populations={}
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_country_code(country_name)
if code:
cc_populations[code]=population
#2.根据人口数量将所有国家分为三组
cc_pops_1,cc_pops_2,cc_pops_3={},{},{}
for cc,pop in cc_populations.items():
if pop<10000000:
cc_pops_1[cc]=pop
if pop<1000000000:
cc_pops_2[cc]=pop
else:
cc_pops_3[cc]=pop
#3.打印每个分组包含多少个国家
print(len(cc_pops_1),len(cc_pops_2),len(cc_pops_3))
#4.设置主题样式
wm_style= LightColorizedStyle
wm = pygal.maps.world.World(style=wm_style)
wm.title = "2010年各国人口总数"
wm.add('0-1000万',cc_pops_1)
wm.add('1000万-10亿',cc_pops_2)
wm.add('大于10亿',cc_pops_3)
wm.render_to_file("world_population.svg")
结果演示: