pygal 绘制地图

版权声明: https://blog.csdn.net/dashoumeixi/article/details/81203601

一个简单的绘制图片模块

其中的 人口数据 可以去http://data.okfn.org 下载 

import json,sys,os,time
from pygal_maps_world.i18n import COUNTRIES
import pygal
#国家名称对应的国家码
maps = {name:code for code, name in COUNTRIES.items()}
#国家码:人口数
data = {}
year = input('年份(1960 - 2014)')

def country_code(name):
    code = None
    try:
        code = maps[name]
    except Exception:
        code = None
    return code

with open('1.json') as fd:
    json_data = json.load(fd)
    data = {country_code(d['Country Name']):int(d['Value']) for d in json_data if d['Year'] == year}
#删除不支持的国家
del data[None]
#创建地图
chart = pygal.maps.world.World()
chart.title = 'fuck map'
# chart.add('China',['cn','jp','tw'])
#可以继续add, 第一个参数label, 第二个可以列表/词典 
chart.add('China',data)
#输出图片s
chart.render_to_file('china.svg')

猜你喜欢

转载自blog.csdn.net/dashoumeixi/article/details/81203601