使用python制作世界地圖
- 前言
- 一、使用install pygal_maps_world
- 1.1 得到兩個字母的國別碼
- 1.2 小實踐:在世界地圖中標出中國
- 1.3 拓展,得到帶人口數的地圖
- 二、顯現世界地圖
- 三、總結
前言
使用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
# 如果沒有找到指定的國家,就回傳None
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 = pop_dict['Value']
population = int(float(pop_dict['Value']))
code = get_country_code(country_name)
if code:
# print(code + ": " + str(population))
cc_populations[code] = population
else:
print('ERROR - ' + country_name)
# print(country_name + ": " + population)
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')
這里只顯示中國和中國人口是因為我將滑鼠放在中國的位置上
后面我們改行程式,從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)
# 函式json.load()將資料轉換為Python能夠處理的格式,這里是一個串列,
# 創建一個國別碼:人口的字典
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
# print(len(cc_1),len(cc_2),len(cc_3))
wm_style = RS('#336699', base_style=LCS)# wm_style=RotateStyle("#447722")
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')
三、總結
這里面得到的中國地圖沒有將臺灣包括進去,原因在于population_data.json這個檔案的問題,我呼吁強烈抵制使用它,
我在之前也沒意識到這個問題,感謝審核大大的提醒,在此我抵制所有侵害我們祖國的人或事,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/289302.html
標籤:python
