前言
使用Python 实现世界地图制作
学习的同时,我想呼吁更多人去抵制一些侵害我国领土的事情
一、使用install pygal_maps_world
1.1 得到两个字母的国别码
首先我们安装pygal_maps_world模块,我使用pip进行安装
pip install pygal_maps_world
再利用COUNTRIES得到指定国家的两位国别码
from pygal_maps_world. i18n import COUNTRIES
def get_country_code ( country_name) :
"""根据指定的国家,返回两个字母的国别码"""
for code, name in COUNTRIES. items( ) :
if name == country_name:
return code
return None
print ( get_country_code( 'China' ) )
这里我们可以达到中国的国别码是cn
接下来我们在世界地图中标出中国
1.2 小实践:在世界地图中标出中国
我们利用install pygal_maps_world在世界地图上标出中国,将其保存为一个svg格式的文件。
import pygal_maps_world. maps
wm = pygal_maps_world. maps . World( )
wm. title = '中国'
wm. add( '中华人民共和国' , [ 'cn' ] )
wm. render_to_file( '中国.svg' )
1.3 拓展,得到带人口数的地图
同样是使用pygal_maps_world,具体程序如下:
import pygal_maps_world. maps
wm = pygal_maps_world. maps. World( )
wm. title = 'Populations of China'
wm. add( 'CHINA' , { 'cn' : 1338300000 } )
wm. render_to_file( 'China_populations.svg' )
二、显现世界地图
首先我们需要下载population_data的json的文件,再使用python导入json文件
通过使用我们实现写好的country_codes文件,从其中调用get_country_code方法,再使用pygal_maps_world绘制一个显示各个国家以及其2010年人口数的地图:
import json
from country_codes import get_country_code
import pygal_maps_world. maps
filename= 'population_data.json'
with open ( filename) as f:
pop_data= json. load( f)
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' ] ) )
code = get_country_code( country_name)
if code:
cc_populations[ code] = population
else :
print ( 'ERROR - ' + country_name)
wm = pygal_maps_world. maps. World( )
wm. title = 'World Population in 2010, by Country'
wm. add( '2010' , cc_populations)
wm. render_to_file( 'world_population.svg' )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
这里只显示中国和中国人口是因为我将鼠标放在中国的位置上
后面我们改进程序,从pygal.style中调用了LightColorizedStyle和RotateStyle方法改变地图颜色和亮度
import json
import pygal
from country_codes import get_country_code
from pygal. style import LightColorizedStyle as LCS, RotateStyle as RS
filename = 'population_data.json'
with open ( filename) as f:
world_maps = json. load( f)
cc_populations = { }
for pop_dict in world_maps:
if pop_dict[ 'Year' ] == '2010' :
country= pop_dict[ 'Country Name' ]
population= int ( float ( pop_dict[ 'Value' ] ) )
code= get_country_code( country)
if code:
cc_populations[ code] = population
cc_1, cc_2, cc_3= { } , { } , { }
for cc, population in cc_populations. items( ) :
if population>= 1000000000 :
cc_1[ cc] = population
elif population< 10000000 :
cc_3[ cc] = population
else :
cc_2[ cc] = population
wm_style = RS( '#336699' , base_style= LCS)
wm = pygal. maps. world. World( style= wm_style)
wm. title = 'World Population in 2010, by Country'
wm. add( '>1bn' , cc_1)
wm. add( '10m-1bn' , cc_2)
wm. add( '0-10m' , cc_3)
wm. render_to_file( '世界地图.svg' )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
三、总结
这里面得到的中国地图没有将台湾包括进去,原因在于population_data.json这个文件的问题,我呼吁强烈抵制使用它。
我在之前也没意识到这个问题,感谢审核大大的提醒,在此我抵制所有侵害我们祖国的人或事。