数据来源
我从腾讯网采集全国的每日疫情数据
https://view.inews.qq.com/g2/getOnsInfo?name=wuwei_ww_cn_day_counts&callback=&_=%d
导包
采集数据所需
import time
import json
import requests
from datetime import datetime
画图所需
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
开始采集数据
"""采集每日疫情数据"""
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'])
遍历所需数据
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']))
开始画图
plt.figure('全国疫情统计图表', facecolor='#f4f4f4', figsize=(10, 8))
plt.title('全国疫情曲线', 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('全国疫情曲线.png') # 保存为文件
plt.show()
最终效果
我的个人微信公众号,欢迎关注哈哈哈!