注:数据来源国家卫建委,各省市卫健委公开数据。
1,全国疫情地图:
从疫情地图来看,确诊病例主要集中在湖北,其次是广东,河南,浙江等省份,其他省份确诊病例都在1000例以下。
import numpy as np
import pandas as pd
from pyecharts.charts import Map
from pyecharts import options as opts
from pyecharts.globals import ThemeTyp
def draw_map():
df = pd.read_excel('ncov_3.xlsx')
provice=df['地区'][:-1] #最后一行是合计,所以不要最后一行的数据
values=df['确诊'][:-1]
value = df[['地区','确诊']][:-1]
data=[]
for i in zip(list(value['地区']),list(value['确诊'])):
data.append(i)
map = (
Map(init_opts=opts.InitOpts(bg_color="#FFFAFA", theme=ThemeType.ESSOS, width=1000))
.add("确诊人数", data,is_map_symbol_show=False)
.set_global_opts(
title_opts=opts.TitleOpts(title="全国疫情图"),
visualmap_opts=opts.VisualMapOpts(
is_piecewise=True, # 设置是否为分段显示
# 自定义的每一段的范围,以及每一段的文字,以及每一段的特别的样式。例如:
pieces=[
{"min": 10000, "label": '>=10000', "color": "#660208"},
{"min": 1000, "max": 9999, "label": '1000-9999', "color": "#8C0D0D"}, # 不指定 max,表示 max 为无限大(Infinity)。
{"min": 100, "max": 999, "label": '100-999', "color": "#CC2929"},
{"min": 10, "max": 99, "label": '10-99', "color": "#FF7B69"},
{"min": 1, "max": 9, "label": '1-9', "color": "#FFAA85"}
],
# 两端的文本,如['High', 'Low']。
# range_text=['高', '低'],
),
)
)
map.render(path="中国疫情人数分级地图1.html")
地图如下:
湖北省(其他省份类似)的疫情地图代码及地图如下:
从湖北省的分布来看,主要集中在武汉,其次是孝感、黄冈、鄂州、随州、荆州、襄阳等地。
def hubei_map():
df3 = pd.read_excel('ncov_4.xlsx')
hubei=df3[df3['地区']=='湖北']
data =[]
lst=[]
city=[]
for i in list(hubei['确诊']):
i=int(i)
lst.append(i)
for i in list(hubei['城市']):
if i[-1]=='区':
i='神农架林区'
elif i=='恩施':
i=i+'土家族苗族自治州'
else:
i = i+'市'
city.append(i)
for i in zip(city,lst):
list(i)
data.append(i)
map = (
Map(init_opts=opts.InitOpts(bg_color="#FFFAFA", theme=ThemeType.ESSOS, width=1000))
.add("确诊人数", data,maptype='湖北',is_map_symbol_show=False)
.set_global_opts(
title_opts=opts.TitleOpts(title="湖北疫情图"),
visualmap_opts=opts.VisualMapOpts(
is_piecewise=True, # 设置是否为分段显示
# 自定义的每一段的范围,以及每一段的文字,以及每一段的特别的样式。例如:
pieces=[
{"min": 10000, "label": '>=10000', "color": "#660208"},
{"min": 1000, "max": 9999, "label": '1000-9999', "color": "#8C0D0D"}, #不指定 max,表示 max 为无限大(Infinity)。
{"min": 100, "max": 999, "label": '100-999', "color": "#CC2929"},
{"min": 10, "max": 99, "label": '10-99', "color": "#FF7B69"},
{"min": 1, "max": 9, "label": '1-9', "color": "#FFAA85"}
],
),
)
)
map.render(path='湖北疫情图.html')
全国新增趋势图:
df = pd.read_excel('ncov_1.xlsx')
x_da = list(df['日期'])
x_data=[]
for i in x_da:
x_data.append(str(i)[:-9])
y_data1 = list(df['确诊病例'])
y_data2 = list(df['疑似病例'])
line= (
Line()
.add_xaxis(x_data)
.add_yaxis("新增确诊", y_data1,is_smooth=True,color='black',label_opts=opts.LabelOpts(is_show=False))
.add_yaxis("新增疑似", y_data2,is_smooth=True,label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(xaxis_opts=opts.AxisOpts(type_='category',boundary_gap=False),
title_opts=opts.TitleOpts(title="全国新增趋势图"),
tooltip_opts=opts.TooltipOpts(trigger='axis',#此处将trigger设置为axis,默认是item,默认无法显示提示悬浮框全部显示
#使用js回调函数实现提示悬浮框显示系列名和分类名及值
formatter=utils.JsCode("""
function(params, ticket, callback){
var htmlStr = '';
for (var i=0;i < params.length;i++){
var param = params[i];
var xName = param.name;
var seriesName = param.seriesName;
var value = param.value[1];
var color = param.color;
if (i===0){htmlStr += xName + '
';}
htmlStr += '';
htmlStr += '';
htmlStr += seriesName + ':' + value ;
htmlStr += '';
}
return htmlStr;
}
"""
))
)
)
line.render_notebook()
全国累计确诊病例及疑似病例
def leiji():
df = pd.read_excel('ncov_2.xlsx')
x_da = list(df['日期'])
x_data=[]
for i in x_da:
x_data.append(str(i)[:-9])
y_data1 = list(df['确诊'])
y_data2 = list(df['疑似'])
line= (
Line()
.add_xaxis(x_data)
.add_yaxis("新增确诊", y_data1,is_smooth=True,color='gray',label_opts=opts.LabelOpts(is_show=False))
.add_yaxis("新增疑似", y_data2,is_smooth=True,label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(xaxis_opts=opts.AxisOpts(type_='category',boundary_gap=False),
title_opts=opts.TitleOpts(title="全国新增趋势图"),
tooltip_opts=opts.TooltipOpts(trigger='axis',
formatter=utils.JsCode("""
function(params, ticket, callback){
var htmlStr = '';
for (var i=0;i < params.length;i++){
var param = params[i];
var xName = param.name;
var seriesName = param.seriesName;
var value = param.value[1];
var color = param.color;
if (i===0){htmlStr += xName + '
';}
htmlStr += '';
htmlStr += '';
htmlStr += seriesName + ':' + value ;
htmlStr += '';
}
return htmlStr;
}
"""
))
)
)
line.render(path='全国累计确诊疑似.html')
leiji()
全国累计治愈人数及死亡人数:
def Cure_or_Die():
df = pd.read_excel('ncov_2.xlsx')
x_da = list(df['日期'])
x_data=[]
for i in x_da:
x_data.append(str(i)[:-9])
y_data1 = list(df['治愈'])
y_data2 = list(df['死亡'])
line= (
Line()
.add_xaxis(x_data)
.add_yaxis("治愈人数", y_data1,is_smooth=True,color='green',label_opts=opts.LabelOpts(is_show=False))
.add_yaxis("死亡人数", y_data2,is_smooth=True,label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(xaxis_opts=opts.AxisOpts(type_='category',boundary_gap=False),
title_opts=opts.TitleOpts(title="治愈及死亡人数"),
tooltip_opts=opts.TooltipOpts(trigger='axis',
formatter=utils.JsCode("""
function(params, ticket, callback){
var htmlStr = '';
for (var i=0;i < params.length;i++){
var param = params[i];
var xName = param.name;
var seriesName = param.seriesName;
var value = param.value[1];
var color = param.color;
if (i===0){htmlStr += xName + '
';}
htmlStr += '';
htmlStr += '';
htmlStr += seriesName + ':' + value ;
htmlStr += '';
}
return htmlStr;
}
"""
))
)
)
line.render(path='全国累计治愈及死亡人数.html')
Cure_or_Die()
治愈率及死亡率:
def Cure_or_Die_Rate():
df = pd.read_excel('ncov_2.xlsx')
x_da = list(df['日期'])
x_data=[]
y_data1=[]
y_data2=[]
for i in x_da:
x_data.append(str(i)[:-9])
y_data1 = list(df['治愈率'])
y_data2 = list(df['死亡率'])
line= (
Line()
.add_xaxis(x_data)
.add_yaxis("治愈率", y_data1,is_smooth=True,color='green',label_opts=opts.LabelOpts(is_show=False))
.add_yaxis("死亡率", y_data2,is_smooth=True,label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(xaxis_opts=opts.AxisOpts(type_='category',boundary_gap=False,max_interval=0.5),#设置Y轴的大小
title_opts=opts.TitleOpts(title="治愈率及死亡率"),
tooltip_opts=opts.TooltipOpts(trigger='axis',
formatter=utils.JsCode("""
function(params, ticket, callback){
var htmlStr = '';
for (var i=0;i < params.length;i++){
var param = params[i];
var xName = param.name;
var seriesName = param.seriesName;
var value = param.value[1];
var color = param.color;
if (i===0){htmlStr += xName + '
';}
htmlStr += '';
htmlStr += '';
htmlStr += seriesName + ':' + (value*100).toFixed(2)+'%' ;
htmlStr += '';
}
return htmlStr;
}
"""
))
)
)
line.render(path='治愈率及死亡率.html')
Cure_or_Die_Rate()
全国/湖北/非湖北新增确诊:
def New_Comparison():
df = pd.read_excel('ncov_5.xlsx')
x_da = list(df['日期'])
x_data=[]
for i in x_da:
x_data.append(str(i)[:-9])
y_data1 = list(df['确诊病例'])
y_data2 = list(df['湖北'])
y_data3 = list(df['非湖北'])
line= (
Line()
.add_xaxis(x_data)
.add_yaxis("全国", y_data1,is_smooth=True,color='green',label_opts=opts.LabelOpts(is_show=False))
.add_yaxis("湖北", y_data2,is_smooth=True,label_opts=opts.LabelOpts(is_show=False))
.add_yaxis("非湖北", y_data3,is_smooth=True,label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(xaxis_opts=opts.AxisOpts(type_='category',boundary_gap=False),
title_opts=opts.TitleOpts(title="全国/湖北/非湖北新增确诊病例趋势"),
tooltip_opts=opts.TooltipOpts(trigger='axis',
formatter=utils.JsCode("""
function(params, ticket, callback){
var htmlStr = '';
for (var i=0;i < params.length;i++){
var param = params[i];
var xName = param.name;
var seriesName = param.seriesName;
var value = param.value[1];
var color = param.color;
if (i===0){htmlStr += xName + '
';}
htmlStr += '';
htmlStr += '';
htmlStr += seriesName + ':' + value ;
htmlStr += '';
}
return htmlStr;
}
"""
))
)
)
line.render(path='新增确诊病例对比.html')
New_Comparison()