Python抓新型冠状病毒肺炎疫情数据并绘制全国疫情分布的代码实例


Posted in Python onFebruary 05, 2020

运行结果(2020-2-4日数据)

Python抓新型冠状病毒肺炎疫情数据并绘制全国疫情分布的代码实例

Python抓新型冠状病毒肺炎疫情数据并绘制全国疫情分布的代码实例

数据来源

news.qq.com/zt2020/page/feiyan.htm

Python抓新型冠状病毒肺炎疫情数据并绘制全国疫情分布的代码实例

抓包分析

Python抓新型冠状病毒肺炎疫情数据并绘制全国疫情分布的代码实例

Python抓新型冠状病毒肺炎疫情数据并绘制全国疫情分布的代码实例

日报数据格式

"chinaDayList": [{
		"date": "01.13",
		"confirm": "41",
		"suspect": "0",
		"dead": "1",
		"heal": "0"
	}, {
		"date": "01.14",
		"confirm": "41",
		"suspect": "0",
		"dead": "1",
		"heal": "0"
	}, {
		"date": "01.15",
		"confirm": "41",
		"suspect": "0",
		"dead": "2",
		"heal": "5"
	}, {
	。。。。。。

全国各地疫情数据格式

"lastUpdateTime": "2020-02-04 12:43:19",
	"areaTree": [{
		"name": "中国",
		"children": [{
			"name": "湖北",
			"children": [{
				"name": "武汉",
				"total": {
					"confirm": 6384,
					"suspect": 0,
					"dead": 313,
					"heal": 303
				},
				"today": {
					"confirm": 1242,
					"suspect": 0,
					"dead": 48,
					"heal": 79
				}
			}, {
				"name": "黄冈",
				"total": {
					"confirm": 1422,
					"suspect": 0,
					"dead": 19,
					"heal": 36
				},
				"today": {
					"confirm": 176,
					"suspect": 0,
					"dead": 2,
					"heal": 9
				}
			}, {
			。。。。。。

地图数据

github.com/dongli/china-shapefiles

代码实现

#%%

import time, json, requests
from datetime import datetime
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.font_manager import FontProperties
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
import numpy as np
import jsonpath

plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号

#%%

# 全国疫情地区分布(省级确诊病例)
def catch_cn_disease_dis():
 timestamp = '%d'%int(time.time()*1000)
 url_area = ('https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
    '&callback=&_=') + timestamp
 world_data = json.loads(requests.get(url=url_area).json()['data'])
 china_data = jsonpath.jsonpath(world_data, 
         expr='$.areaTree[0].children[*]')
 list_province = jsonpath.jsonpath(china_data, expr='$[*].name')
 list_province_confirm = jsonpath.jsonpath(china_data, expr='$[*].total.confirm')
 dic_province_confirm = dict(zip(list_province, list_province_confirm)) 
 return dic_province_confirm

area_data = catch_cn_disease_dis()
print(area_data)

#%%

# 抓取全国疫情按日期分布
'''
数据源:
"chinaDayList": [{
		"date": "01.13",
		"confirm": "41",
		"suspect": "0",
		"dead": "1",
		"heal": "0"
	}, {
		"date": "01.14",
		"confirm": "41",
		"suspect": "0",
		"dead": "1",
		"heal": "0"
	}
'''
def catch_cn_daily_dis():
 timestamp = '%d'%int(time.time()*1000)
 url_area = ('https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
    '&callback=&_=') + timestamp
 world_data = json.loads(requests.get(url=url_area).json()['data'])
 china_daily_data = jsonpath.jsonpath(world_data, 
         expr='$.chinaDayList[*]')

 # 其实没必要单独用list存储,json可读性已经很好了;这里这样写仅是为了少该点老版本的代码  
 list_dates = list() # 日期
 list_confirms = list() # 确诊
 list_suspects = list() # 疑似
 list_deads = list() # 死亡
 list_heals = list() # 治愈  
 for item in china_daily_data:
  month, day = item['date'].split('.')
  list_dates.append(datetime.strptime('2020-%s-%s'%(month, day), '%Y-%m-%d'))
  list_confirms.append(int(item['confirm']))
  list_suspects.append(int(item['suspect']))
  list_deads.append(int(item['dead']))
  list_heals.append(int(item['heal']))  
 return list_dates, list_confirms, list_suspects, list_deads, list_heals  

list_date, list_confirm, list_suspect, list_dead, list_heal = catch_cn_daily_dis() 
print(list_date)
 

#%%

# 绘制每日确诊和死亡数据
def plot_cn_daily():
 # list_date, list_confirm, list_suspect, list_dead, list_heal = catch_cn_daily_dis() 
 
 plt.figure('novel coronavirus', facecolor='#f4f4f4', figsize=(10, 8))
 plt.title('全国新型冠状病毒疫情曲线', fontsize=20)
 print('日期元素数:', len(list_date), "\n确诊元素数:", len(list_confirm))
 plt.plot(list_date, list_confirm, label='确诊')
 plt.plot(list_date, list_suspect, label='疑似')
 plt.plot(list_date, list_dead, label='死亡')
 plt.plot(list_date, list_heal, label='治愈')
 xaxis = plt.gca().xaxis 
 # x轴刻度为1天
 xaxis.set_major_locator(matplotlib.dates.DayLocator(bymonthday=None, interval=1, tz=None))
 xaxis.set_major_formatter(mdates.DateFormatter('%m月%d日'))
 plt.gcf().autofmt_xdate() # 优化标注(自动倾斜)
 plt.grid(linestyle=':') # 显示网格
 plt.xlabel('日期',fontsize=16)
 plt.ylabel('人数',fontsize=16)
 plt.legend(loc='best')
 
plot_cn_daily()

#%%

# 绘制全国省级行政区域确诊分布图
count_iter = 0
def plot_cn_disease_dis():
 # area_data = catch_area_distribution()
 font = FontProperties(fname='res/coure.fon', size=14)
 
 # 经纬度范围
 lat_min = 10 # 纬度
 lat_max = 60
 lon_min = 70 # 经度
 lon_max = 140
  
 # 标签颜色和文本 
 legend_handles = [
    matplotlib.patches.Patch(color='#7FFFAA', alpha=1, linewidth=0),
    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),
 ]
 legend_labels = ['0人', '1-10人', '11-100人', '101-1000人', '>1000人']

 fig = plt.figure(facecolor='#f4f4f4', figsize=(10, 8)) 
 # 新建区域
 axes = fig.add_axes((0.1, 0.1, 0.8, 0.8)) # left, bottom, width, height, figure的百分比,从figure 10%的位置开始绘制, 宽高是figure的80%
 axes.set_title('全国新型冠状病毒疫情地图(确诊)', fontsize=20) # fontproperties=font 设置失败 
 # bbox_to_anchor(num1, num2), num1用于控制legend的左右移动,值越大越向右边移动,num2用于控制legend的上下移动,值越大,越向上移动。
 axes.legend(legend_handles, legend_labels, bbox_to_anchor=(0.5, -0.11), loc='lower center', ncol=5) # prop=font
 
 china_map = Basemap(llcrnrlon=lon_min, urcrnrlon=lon_max, llcrnrlat=lat_min, urcrnrlat=lat_max, resolution='l', ax=axes)
 # labels=[True,False,False,False] 分别代表 [left,right,top,bottom]
 china_map.drawparallels(np.arange(lat_min,lat_max,10), labels=[1,0,0,0]) # 画经度线
 china_map.drawmeridians(np.arange(lon_min,lon_max,10), labels=[0,0,0,1]) # 画纬度线
 china_map.drawcoastlines(color='black') # 洲际线
 china_map.drawcountries(color='red') # 国界线
 china_map.drawmapboundary(fill_color = 'aqua')
 # 画中国国内省界和九段线
 china_map.readshapefile('res/china-shapefiles-master/china', 'province', drawbounds=True)
 china_map.readshapefile('res/china-shapefiles-master/china_nine_dotted_line', 'section', drawbounds=True)
 
 global count_iter
 count_iter = 0
 
 # 内外循环不能对调,地图中每个省的数据有多条(绘制每一个shape,可以去查一下第一条“台湾省”的数据)
 for info, shape in zip(china_map.province_info, china_map.province):
  pname = info['OWNER'].strip('\x00')
  fcname = info['FCNAME'].strip('\x00')
  if pname != fcname: # 不绘制海岛
   continue
  is_reported = False # 西藏没有疫情,数据源就不取不到其数据 
  for prov_name in area_data.keys():    
   count_iter += 1
   if prov_name in pname:
    is_reported = True
    if area_data[prov_name] == 0:
     color = '#f0f0f0'
    elif area_data[prov_name] <= 10:
     color = '#ffaa85'
    elif area_data[prov_name] <= 100:
     color = '#ff7b69'
    elif area_data[prov_name] <= 1000:
     color = '#bf2121'
    else:
     color = '#7f1818'
    break
   
  if not is_reported:
   color = '#7FFFAA'
   
  poly = Polygon(shape, facecolor=color, edgecolor=color)
  axes.add_patch(poly)
  

plot_cn_disease_dis()
print('迭代次数', count_iter)

以上就是三水点靠木小编整理的全部知识点内容,感谢大家的学习和对三水点靠木的支持。

Python 相关文章推荐
用Python创建声明性迷你语言的教程
Apr 13 Python
基于python3 类的属性、方法、封装、继承实例讲解
Sep 19 Python
python3+PyQt5泛型委托详解
Apr 24 Python
python 编码规范整理
May 05 Python
python样条插值的实现代码
Dec 17 Python
利用Python正则表达式过滤敏感词的方法
Jan 21 Python
Python面向对象程序设计类的多态用法详解
Apr 12 Python
python自定义时钟类、定时任务类
Feb 22 Python
python实现矩阵和array数组之间的转换
Nov 29 Python
python实现高斯投影正反算方式
Jan 17 Python
python 还原梯度下降算法实现一维线性回归
Oct 22 Python
用python对excel查重
Dec 07 Python
Python实现新型冠状病毒传播模型及预测代码实例
Feb 05 #Python
基于Tensorflow批量数据的输入实现方式
Feb 05 #Python
Python操作注册表详细步骤介绍
Feb 05 #Python
Python类继承和多态原理解析
Feb 05 #Python
Python模块 _winreg操作注册表
Feb 05 #Python
python3操作注册表的方法(Url protocol)
Feb 05 #Python
Python tkinter模版代码实例
Feb 05 #Python
You might like
JAVA/JSP学习系列之六
2006/10/09 PHP
php cookie 作用范围?不要在当前页面使用你的cookie
2009/03/24 PHP
PHP生成不同颜色、不同大小的tag标签函数
2013/09/23 PHP
jquery+php+ajax显示上传进度的多图片上传并生成缩略图代码
2014/10/15 PHP
php源码分析之DZX1.5随机数函数random用法
2015/06/17 PHP
PHP面向对象程序设计(OOP)之方法重写(override)操作示例
2018/12/21 PHP
javascript 的Document属性和方法集合
2010/01/25 Javascript
Extjs中使用extend(js继承) 的代码
2012/03/15 Javascript
JavaScript异步编程Promise模式的6个特性
2014/04/03 Javascript
推荐 21 款优秀的高性能 Node.js 开发框架
2014/08/18 Javascript
jQuery设置指定网页元素宽度和高度的方法
2015/03/25 Javascript
Javascript闭包实例详解
2015/11/29 Javascript
前端设计师们最常用的JS代码汇总
2016/09/25 Javascript
javascript中活灵活现的Array对象详解
2016/11/30 Javascript
详解nodejs微信公众号开发——3.封装消息响应模块
2017/04/10 NodeJs
详解Vue 普通对象数据更新与 file 对象数据更新
2017/04/26 Javascript
js实现城市级联菜单的2种方法
2017/06/23 Javascript
手把手教你如何使用nodejs编写cli命令行
2018/11/05 NodeJs
Nodejs中怎么实现函数的串行执行
2019/03/02 NodeJs
[46:25]DOTA2上海特级锦标赛主赛事日 - 4 败者组第五轮 MVP.Phx VS EG第二局
2016/03/05 DOTA
Python3中的2to3转换工具使用示例
2015/06/12 Python
Python实现决策树C4.5算法的示例
2018/05/30 Python
python递归全排列实现方法
2018/08/18 Python
Python3.5内置模块之time与datetime模块用法实例分析
2019/04/27 Python
对python中GUI,Label和Button的实例详解
2019/06/27 Python
python实现对图片进行旋转,放缩,裁剪的功能
2019/08/07 Python
使用Python生成200个激活码的实现方法
2019/11/22 Python
Python reshape的用法及多个二维数组合并为三维数组的实例
2020/02/07 Python
python初步实现word2vec操作
2020/06/09 Python
python os模块在系统管理中的应用
2020/06/22 Python
药学专业大学生自荐信
2013/09/28 职场文书
英文版网络工程师求职信
2013/10/28 职场文书
小区门卫岗位职责
2013/12/31 职场文书
统计系教授推荐信
2014/02/28 职场文书
2014年应急工作总结
2014/12/11 职场文书
高通2023 年将发布高性能PC处理器
2022/04/29 数码科技