近来武汉肺炎肆虐全国,大多人的日常应该是宅在家里。出于好奇,笔者想用Python来绘制中国2020肺炎疫情地图。
本代码采用Python3,需要安装模块:pyecharts和echarts-china-provinces-pypkg。
Python代码如下:
# -*- coding: utf-8 -*-
# author: Jclian91
# time: 2020-01-29 11:37
# -*- coding: utf-8 -*-
# author: Jclian91
# time: 2020-01-29 11:37
from pyecharts.charts import Map
from pyecharts import options as opts
# 省和直辖市
province_distribution = {'湖北':3554, '浙江':296, '广东': 241,
'湖南':221, '河南':206, '安徽': 152,
'重庆':147, '山东':121, '江西': 109,
'四川':108, '江苏':99, '北京':91,
'福建':82, '上海':80, '广西':58,
'陕西':56, '河北':48, '云南':44,
'海南':43, '黑龙江':37, '辽宁':36,
'山西':27, '天津':25, '甘肃':24,
'内蒙古':16, '*':13, '宁夏':12,
'贵州':9, '吉林':9, '*':8,
'香港':8, '澳门':7, '青海':6,
'*':0
}
# maptype='china' 只显示全国直辖市和省级
map = Map()
map.set_global_opts(
title_opts=opts.TitleOpts(title="20200129中国疫情地图"),
visualmap_opts=opts.VisualMapOpts(max_=3600, is_piecewise=True,
pieces=[
{"max": 5000, "min": 1001, "label": ">1000", "color": "#8A0808"},
{"max": 1000, "min": 500, "label": "500-1000", "color": "#B40404"},
{"max": 499, "min": 100, "label": "100-499", "color": "#DF0101"},
{"max": 99, "min": 10, "label": "10-99", "color": "#F78181"},
{"max": 9, "min": 1, "label": "1-9", "color": "#F5A9A9"},
{"max": 0, "min": 0, "label": "0", "color": "#FFFFFF"},
], ) #最大数据范围,分段
)
map.add("20200129中国疫情地图", data_pair=province_distribution.items(), maptype="china", is_roam=True)
map.render('20200129中国疫情地图.html')
from pyecharts.charts import Map
from pyecharts import options as opts
# 省和直辖市
province_distribution = {'湖北':3554, '浙江':296, '广东': 241,
'湖南':221, '河南':206, '安徽': 152,
'重庆':147, '山东':121, '江西': 109,
'四川':108, '江苏':99, '北京':91,
'福建':82, '上海':80, '广西':58,
'陕西':56, '河北':48, '云南':44,
'海南':43, '黑龙江':37, '辽宁':36,
'山西':27, '天津':25, '甘肃':24,
'内蒙古':16, '*':13, '宁夏':12,
'贵州':9, '吉林':9, '*':8,
'香港':8, '澳门':7, '青海':6,
'*':0
}
# maptype='china' 只显示全国直辖市和省级
map = Map()
map.set_global_opts(
title_opts=opts.TitleOpts(title="20200129中国疫情地图"),
visualmap_opts=opts.VisualMapOpts(max_=3600, is_piecewise=True,
pieces=[
{"max": 5000, "min": 1001, "label": ">1000", "color": "#8A0808"},
{"max": 1000, "min": 500, "label": "500-1000", "color": "#B40404"},
{"max": 499, "min": 100, "label": "100-499", "color": "#DF0101"},
{"max": 99, "min": 10, "label": "10-99", "color": "#F78181"},
{"max": 9, "min": 1, "label": "1-9", "color": "#F5A9A9"},
{"max": 0, "min": 0, "label": "0", "color": "#FFFFFF"},
], ) #最大数据范围,分段
)
map.add("20200129中国疫情地图", data_pair=province_distribution.items(), maptype="china", is_roam=True)
map.render('20200129中国疫情地图.html')
注意,代码中的数据为截止到2020年1月29日中午12点整的全国各省的确诊人数,*人数应为0。
运行上述代码,会生成20200129中国疫情地图.html,用浏览器打开该HTML文件,如下:
在地图上可以进行一些互动,比如点击某个省份,我们可以看到该省份的数据;再比如我们取消“>1000”这一选项,就会发现湖北省颜色为白色,如下图:
感谢大家阅读,武汉加油!湖北加油!祝大家都身体健康!