今天,群里白垩老师问如何用python画武汉肺炎疫情地图。白垩老师是研究海洋生态与地球生物的学者,国家重点实验室成员,于不惑之年学习python,实为我等学习楷模。先前我并没有关注武汉肺炎的具体数据,也没有画过类似的数据分布图。于是就拿了两个小时,专门研究了一下,遂成此文。
2月6日追记:本文发布后,腾讯的数据源多次变更url和数据格式,导致代码无法运行。有很多热心朋友已经留言,帮助修正了代码,现将这些修正补充到正文中。所有数据抓取的截图均未变更,或有不符,请各位朋友明鉴。另有朋友咨询如何在分省地图上显示各省名字,这次一并补充到代码中。
网上一搜,首先搜到的是腾讯的疫情实时追踪,那就用这个数据源吧。
有了网址怎么抓数据呢?这里,我送大家一双火眼金睛,可以从纷乱中找到最靠谱的下载方式。我习惯用FireFox浏览器,下面的讲解就以FireFox为例(其他浏览器基本类似)。
>>> import time, json, requests
>>> url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%d'%int(time.time()*1000)
>>> data = json.loads(requests.get(url=url).json()['data'])
只要两行代码,就可以抓到数据了。怎么样,是不是超级简单?我们在来看看数据结构:
>>> data.keys()
dict_keys(['chinaTotal', 'chinaAdd', 'lastUpdateTime', 'areaTree', 'chinaDayList', 'chinaDayAddList', 'isShowAdd'])
>>> d = data['areaTree'][0]['children']
>>> len(d)
34
>>> [item['name'] for item in d]
['湖北', '浙江', '广东', '河南', '湖南', '江西', '安徽', '重庆', '山东', '江苏', '四川', '上海', '北京', '福建', '黑龙江', '广西', '陕西', '河北', '云南', '海南', '山西', '辽宁', '天津', '贵州', '甘肃', '吉林', '内蒙古', '宁夏', '新疆', '香港', '青海', '台湾', '澳门', '西藏']
>>> d[0]['children']
[{'name': '武汉', 'total': {'confirm': 10117, 'suspect': 0, 'dead': 414, 'heal': 431}, 'today': {'confirm': 1766, 'suspect': 0, 'dead': 52, 'heal': 58}}, {'name': '孝感', 'total': {'confirm': 1886, 'suspect': 0, 'dead': 25, 'heal': 9}, 'today': {'confirm': 424, 'suspect': 0, 'dead': 7, 'heal': 3}}, {'name': '黄冈', 'total': {'confirm': 1807, 'suspect': 0, 'dead': 29, 'heal': 60}, 'today': {'confirm': 162, 'suspect': 0, 'dead': 4, 'heal': 8}}, {'name': '随州', 'total': {'confirm': 834, 'suspect': 0, 'dead': 9, 'heal': 9}, 'today': {'confirm': 128, 'suspect': 0, 'dead': 1, 'heal': 0}}, {'name': '荆州', 'total': {'confirm': 801, 'suspect': 0, 'dead': 10, 'heal': 18}, 'today': {'confirm': 88, 'suspect': 0, 'dead': 1, 'heal': 6}}, {'name': '襄阳', 'total': {'confirm': 787, 'suspect': 0, 'dead': 2, 'heal': 10}, 'today': {'confirm': 52, 'suspect': 0, 'dead': 0, 'heal': 3}}, {'name': '黄石', 'total': {'confirm': 566, 'suspect': 0, 'dead': 2, 'heal': 25}, 'today': {'confirm': 57, 'suspect': 0, 'dead': 0, 'heal': 7}}, {'name': '宜昌', 'total': {'confirm': 563, 'suspect': 0, 'dead': 6, 'heal': 9}, 'today': {'confirm': 67, 'suspect': 0, 'dead': 2, 'heal': 0}}, {'name': '荆门', 'total': {'confirm': 508, 'suspect': 0, 'dead': 17, 'heal': 21}, 'today': {'confirm': 86, 'suspect': 0, 'dead': 1, 'heal': 5}}, {'name': '鄂州', 'total': {'confirm': 423, 'suspect': 0, 'dead': 18, 'heal': 8}, 'today': {'confirm': 41, 'suspect': 0, 'dead': 0, 'heal': 2}}, {'name': '咸宁', 'total': {'confirm': 399, 'suspect': 0, 'dead': 1, 'heal': 3}, 'today': {'confirm': 15, 'suspect': 0, 'dead': 1, 'heal': 1}}, {'name': '十堰', 'total': {'confirm': 353, 'suspect': 0, 'dead': 0, 'heal': 14}, 'today': {'confirm': 35, 'suspect': 0, 'dead': 0, 'heal': 5}}, {'name': '仙桃', 'total': {'confirm': 265, 'suspect': 0, 'dead': 5, 'heal': 0}, 'today': {'confirm': 40, 'suspect': 0, 'dead': 1, 'heal': 0}}, {'name': '恩施州', 'total': {'confirm': 144, 'suspect': 0, 'dead': 0, 'heal': 10}, 'today': {'confirm': 6, 'suspect': 0, 'dead': 0, 'heal': 4}}, {'name': '天门', 'total': {'confirm': 138, 'suspect': 0, 'dead': 10, 'heal': 1}, 'today': {'confirm': 10, 'suspect': 0, 'dead': 0, 'heal': 1}}, {'name': '潜江', 'total': {'confirm': 64, 'suspect': 0, 'dead': 1, 'heal': 0}, 'today': {'confirm': 10, 'suspect': 0, 'dead': 0, 'heal': 0}}, {'name': '神农架', 'total': {'confirm': 10, 'suspect': 0, 'dead': 0, 'heal': 2}, 'today': {'confirm': 0, 'suspect': 0, 'dead': 0, 'heal': 0}}, {'name': '地区待确认', 'total': {'confirm': 0, 'suspect': 0, 'dead': 0, 'heal': 3}, 'today': {'confirm': 0, 'suspect': 0, 'dead': 0, 'heal': 0}}]
3. 数据处理
以省为单位画疫情图,我们只需要统计同属一个省的所有地市的确诊数据即可。最终的数据抓取代码如下:
import time, json, requests
def catch_distribution():
"""抓取行政区域确诊分布数据"""
data = {}
url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%d'%int(time.time()*1000)
for item in json.loads(requests.get(url=url).json()['data'])['areaTree'][0]['children']:
if item['name'] not in data:
data.update({item['name']:0})
for city_data in item['children']:
data[item['name']] += int(city_data['total']['confirm'])
return data
4. 数据可视化
数据可视化,我习惯使用matplotlib模块。matplotlib有很多扩展工具包(toolkits),比如,画3D需要mplot3d工具包,画地图的话,则需要basemap工具包,以及处理地图投影的pyproj模块。另外画海陆分界线、国界线、行政分界线等还需要shape数据。所需模块请自行安装,shape文件可以从这里下载,绘图用到的矢量字库可以从自己的电脑上随便找一个(我用的是simsun.ttf)。我的主程序是2019nCoV.py,shape文件下载下来之后,是这样保存的:
以下为全部代码,除了疫情地图,还包括了全国每日武汉肺炎确诊数据的下载和可视化。
# -*- coding: utf-8 -*-
import time
import json
import requests
from datetime import datetime
import numpy as np
import matplotlib
import matplotlib.figure
from matplotlib.font_manager import FontProperties
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
plt.rcParams['font.sans-serif'] = ['FangSong'] # 设置默认字体
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像时'-'显示为方块的问题
def catch_daily():
"""抓取每日确诊和死亡数据"""
url = 'https://view.inews.qq.com/g2/getOnsInfo?name=wuwei_ww_cn_day_counts&callback=&_=%d'%int(time.time()*1000)
data = json.loads(requests.get(url=url).json()['data'])
data.sort(key=lambda x:x['date'])
date_list = list() # 日期
confirm_list = list() # 确诊
suspect_list = list() # 疑似
dead_list = list() # 死亡
heal_list = list() # 治愈
for item in data:
month, day = item['date'].split('/')
date_list.append(datetime.strptime('2020-%s-%s'%(month, day), '%Y-%m-%d'))
confirm_list.append(int(item['confirm']))
suspect_list.append(int(item['suspect']))
dead_list.append(int(item['dead']))
heal_list.append(int(item['heal']))
return date_list, confirm_list, suspect_list, dead_list, heal_list
def catch_distribution():
"""抓取行政区域确诊分布数据"""
data = {}
url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%d'%int(time.time()*1000)
for item in json.loads(requests.get(url=url).json()['data'])['areaTree'][0]['children']:
if item['name'] not in data:
data.update({item['name']:0})
for city_data in item['children']:
data[item['name']] += int(city_data['total']['confirm'])
return data
def plot_daily():
"""绘制每日确诊和死亡数据"""
date_list, confirm_list, suspect_list, dead_list, heal_list = catch_daily() # 获取数据
plt.figure('2019-nCoV疫情统计图表', facecolor='#f4f4f4', figsize=(10, 8))
plt.title('2019-nCoV疫情曲线', fontsize=20)
plt.plot(date_list, confirm_list, label='确诊')
plt.plot(date_list, suspect_list, label='疑似')
plt.plot(date_list, dead_list, label='死亡')
plt.plot(date_list, heal_list, label='治愈')
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m-%d')) # 格式化时间轴标注
plt.gcf().autofmt_xdate() # 优化标注(自动倾斜)
plt.grid(linestyle=':') # 显示网格
plt.legend(loc='best') # 显示图例
plt.savefig('2019-nCoV疫情曲线.png') # 保存为文件
#plt.show()
def plot_distribution():
"""绘制行政区域确诊分布数据"""
data = catch_distribution()
font_14 = FontProperties(fname='res/simsun.ttf', size=14)
font_11 = FontProperties(fname='res/simsun.ttf', size=11)
width = 1600
height = 800
rect = [0.1, 0.12, 0.8, 0.8]
lat_min = 0
lat_max = 60
lon_min = 77
lon_max = 140
'''全球等经纬投影模式使用以下设置,否则使用上面的对应设置
width = 3000
height = 1500
rect = [0, 0, 1, 1]
lat_min = -90
lat_max = 90
lon_min = 0
lon_max = 360
'''
handles = [
matplotlib.patches.Patch(color='#ffaa85', alpha=1, linewidth=0),
matplotlib.patches.Patch(color='#ff7b69', alpha=1, linewidth=0),
matplotlib.patches.Patch(color='#bf2121', alpha=1, linewidth=0),
matplotlib.patches.Patch(color='#7f1818', alpha=1, linewidth=0),
]
labels = [ '1-9人', '10-99人', '100-999人', '>1000人']
provincePos = {
"辽宁省":[121.7,40.9],
"吉林省":[124.5,43.5],
"黑龙江省":[125.6,46.5],
"北京市":[116.0,39.9],
"天津市":[117.0,38.7],
"内蒙古自治区":[110.0,41.5],
"宁夏回族自治区":[105.2,37.0],
"山西省":[111.0,37.0],
"河北省":[114.0,37.8],
"山东省":[116.5,36.0],
"河南省":[111.8,33.5],
"陕西省":[107.5,33.5],
"湖北省":[111.0,30.5],
"江苏省":[119.2,32.5],
"安徽省":[115.5,31.8],
"上海市":[121.0,31.0],
"湖南省":[110.3,27.0],
"江西省":[114.0,27.0],
"浙江省":[118.8,28.5],
"福建省":[116.2,25.5],
"广东省":[113.2,23.1],
"台湾省":[120.5,23.5],
"海南省":[108.0,19.0],
"广西壮族自治区":[107.3,23.0],
"重庆市":[106.5,29.5],
"云南省":[101.0,24.0],
"贵州省":[106.0,26.5],
"四川省":[102.0,30.5],
"甘肃省":[103.0,35.0],
"青海省":[95.0,35.0],
"新疆维吾尔自治区":[85.5,42.5],
"西藏自治区":[85.0,31.5],
"香港特别行政区":[115.1,21.2],
"澳门特别行政区":[112.5,21.2]
}
fig = matplotlib.figure.Figure()
fig.set_size_inches(width/100, height/100) # 设置绘图板尺寸
axes = fig.add_axes(rect)
# 兰博托投影模式,局部
m = Basemap(projection='lcc', llcrnrlon=77, llcrnrlat=14, urcrnrlon=140, urcrnrlat=51, lat_1=33, lat_2=45, lon_0=100, ax=axes)
# 兰博托投影模式,全图
#m = Basemap(projection='lcc', llcrnrlon=80, llcrnrlat=0, urcrnrlon=140, urcrnrlat=51, lat_1=33, lat_2=45, lon_0=100, ax=axes)
# 圆柱投影模式,局部
#m = Basemap(llcrnrlon=lon_min, urcrnrlon=lon_max, llcrnrlat=lat_min, urcrnrlat=lat_max, resolution='l', ax=axes)
# 正射投影模式
#m = Basemap(projection='ortho', lat_0=36, lon_0=102, resolution='l', ax=axes)
# 全球等经纬投影模式,
#m = Basemap(llcrnrlon=lon_min, urcrnrlon=lon_max, llcrnrlat=lat_min, urcrnrlat=lat_max, resolution='l', ax=axes)
#m.etopo()
m.readshapefile('res/china-shapefiles-master/china', 'province', drawbounds=True)
m.readshapefile('res/china-shapefiles-master/china_nine_dotted_line', 'section', drawbounds=True)
m.drawcoastlines(color='black') # 洲际线
m.drawcountries(color='black') # 国界线
m.drawparallels(np.arange(lat_min,lat_max,10), labels=[1,0,0,0]) #画经度线
m.drawmeridians(np.arange(lon_min,lon_max,10), labels=[0,0,0,1]) #画纬度线
pset = set()
for info, shape in zip(m.province_info, m.province):
pname = info['OWNER'].strip('\x00')
fcname = info['FCNAME'].strip('\x00')
if pname != fcname: # 不绘制海岛
continue
for key in data.keys():
if key in pname:
if data[key] == 0:
color = '#f0f0f0'
elif data[key] < 10:
color = '#ffaa85'
elif data[key] <100:
color = '#ff7b69'
elif data[key] < 1000:
color = '#bf2121'
else:
color = '#7f1818'
break
poly = Polygon(shape, facecolor=color, edgecolor=color)
axes.add_patch(poly)
pos = provincePos[pname]
text = pname.replace("自治区", "").replace("特别行政区", "").replace("壮族", "").replace("维吾尔", "").replace("回族", "").replace("省", "").replace("市", "")
if text not in pset:
x, y = m(pos[0], pos[1])
axes.text(x, y, text, fontproperties=font_11, color='#00FFFF')
pset.add(text)
axes.legend(handles, labels, bbox_to_anchor=(0.5, -0.11), loc='lower center', ncol=4, prop=font_14)
axes.set_title("2019-nCoV疫情地图", fontproperties=font_14)
FigureCanvasAgg(fig)
fig.savefig('2019-nCoV疫情地图.png')
if __name__ == '__main__':
plot_daily()
plot_distribution()
2019-nCoV疫情曲线:
2019-nCoV疫情地图(兰勃托投影):
2019-nCoV疫情地图(圆柱投影):
2019-nCoV疫情地图(正射投影):
2019-nCoV疫情地图(全球等经纬投影模式):