用python画新型冠状病毒病例曲线图
地图的画,因为要下载地图工具包,所以没去深入学习。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
功能:用python画新型冠状病毒病例曲线图
环境:python3.7
日期:2020/2/8 21:20
作者:指尖魔法师
版本:1.0
"""
import requests, json
from matplotlib import pyplot as plt
from matplotlib import font_manager
import time
url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%d' % int(time.time()*1000)
def chinaDayList():
"""中国每日数据"""
global url
date_list = list()
confirm_list =list()
suspect_list = list()
dead_list = list()
heal_list = list()
deadRate_list =list()
healRate_list = list()
jsondata = json.loads(requests.get(url).json()['data'])
# print(jsondata['chinaDayList'])
for daydata in jsondata['chinaDayList']:
# print(daydata)
date_list.append(daydata['date'])
confirm_list.append(daydata['confirm'])
suspect_list.append(daydata['suspect'])
dead_list.append(daydata['dead'])
heal_list.append(daydata['heal'])
deadRate_list.append(float(daydata['deadRate']))
healRate_list.append(float(daydata['healRate']))
return date_list, confirm_list, suspect_list, dead_list, heal_list, deadRate_list, healRate_list
def catch_provincedata():
"""获取中国每个省份确诊数据"""
global url
data = dict()
jsondata = json.loads(requests.get(url).json()['data'])
# 中国各省份
provinces = jsondata['areaTree'][0]['children']
for province in provinces:
data.update({province['name']: province['total']['confirm']})
return data
def draw_perday_confirm_suspect(date_list, confirm_list, suspect_list):
"""绘制确诊和疑似人数趋势图"""
# 设置字体
my_font = font_manager.FontProperties('Microsoft YaHei', size=14)
# 设置图形大小
plt.figure(figsize=(12, 8))
# 绘制图表
plt.plot(date_list, confirm_list, label='确诊人数')
plt.plot(date_list, suspect_list, label='疑似人数')
plt.scatter(date_list, confirm_list)
plt.scatter(date_list, suspect_list)
# 设置X轴刻度
x = date_list
xtick_labels = date_list
plt.xticks(x, xtick_labels, rotation=45)
# 添加描述信息
plt.xlabel("日期", fontproperties=my_font)
plt.ylabel("人数", fontproperties=my_font)
plt.title("2019-nCoV每日累计确诊和疑似人数趋势图", fontproperties=my_font)
# 显示辅助网格
plt.grid(alpha=0.4)
# 添加图例
plt.legend(prop=my_font)
plt.savefig('./2019-nCoV每日累计确诊和疑似人数趋势图.png')
plt.show()
def draw_perday_dead_heal(date_list, dead_list, heal_list):
"""绘制治愈和死亡人数趋势图"""
# 设置字体
my_font = font_manager.FontProperties('Microsoft YaHei', size=14)
# 设置图形大小
plt.figure(figsize=(12, 8))
# 绘制图表
plt.plot(date_list, dead_list, label='死亡人数')
plt.plot(date_list, heal_list, label='治愈人数')
plt.scatter(date_list, dead_list)
plt.scatter(date_list, heal_list)
# 设置X轴刻度
x = date_list
xtick_labels = date_list
plt.xticks(x, xtick_labels, rotation=45)
# 添加描述信息
plt.xlabel("日期", fontproperties=my_font)
plt.ylabel("人数", fontproperties=my_font)
plt.title("2019-nCoV每日死亡和治愈人数趋势图", fontproperties=my_font)
# 显示辅助网格
plt.grid(alpha=0.4)
# 添加图例
plt.legend(prop=my_font)
plt.savefig('./2019-nCoV每日死亡和治愈人数趋势图.png')
plt.show()
def draw_perday_deadRate_healRate(date_list, deadRate_list, healRate_list):
"""绘制治愈率和死亡率对比趋势图"""
# 设置字体
my_font = font_manager.FontProperties('Microsoft YaHei', size=14)
# 设置图形大小
plt.figure(figsize=(12, 8))
# 绘制图表
plt.plot(date_list, deadRate_list, label='死亡率')
plt.plot(date_list, healRate_list, label='治愈率')
plt.scatter(date_list, deadRate_list)
plt.scatter(date_list, healRate_list)
# 设置X轴刻度
x = date_list
xtick_labels = date_list
plt.xticks(x, xtick_labels, rotation=45)
y = [i for i in range(int(max(deadRate_list+healRate_list)+2))]
ytick_labels = ["{}%".format(i) for i in range(int(max(deadRate_list+healRate_list)+2))]
plt.yticks(y, ytick_labels)
# 添加描述信息
plt.xlabel("日期", fontproperties=my_font)
plt.ylabel("百分比", fontproperties=my_font)
plt.title("2019-nCoV每日死亡率和治愈率对比趋势图", fontproperties=my_font)
# 显示辅助网格
plt.grid(alpha=0.4)
# 添加图例
plt.legend(prop=my_font)
plt.savefig('./2019-nCoV每日治愈率和死亡率对比趋势图.png')
plt.show()
if __name__=="__main__":
# 获取每日数据
date_list, confirm_list, suspect_list, dead_list, heal_list, deadRate_list, healRate_list = chinaDayList()
# 绘制确诊和疑似人数趋势图
draw_perday_confirm_suspect(date_list, confirm_list, suspect_list)
# 绘制治愈和死亡人数趋势图
draw_perday_dead_heal(date_list, dead_list, heal_list)
# 绘制治愈率和死亡率对比趋势图
draw_perday_deadRate_healRate(date_list, deadRate_list, healRate_list)