Python项目实战 —— 02. 疫情前后全国人口流动可视化大屏
大家可以关注知乎或微信公众号的share16,我们也会同步更新此文章。
一、背景
?? 人口的流动性是造成新型冠状传染病毒在全国范围内扩散的重要驱动因素,很多用于评估传染病毒的感染速率和扩散规模的预测模型均基于人流数据展开,此次因病毒传染而封城和春节期间取消春节活动等措施更是说明了限制人口流动是抑制病毒传播的重要途径。 ?? 春节前的人口大迁徙无疑加速了本次疫情的时空传播速率,而春节后全国范围内的人口回流仍然会对疫情的防控带来巨大的挑战。可以说,深刻认识春节后人口回流的迁徙特征和规律,将对此次疫情的防控起到非常积极的作用。数据时间为2019-11-30和2020-02-20,分别是疫情前和疫情开始第一个春节。
?? 点此下载数据集
二、数据分析
2.1 数据处理
① 疫情前各省人口流动情况——柱形图/地图;广东湖北流动最大的前五个省份飞行图(依据广东是人口流动最大省份、湖北是第一个封城的省份); ② 疫情后各省人口流动情况——柱形图/地图;广东湖北流动最大的前五个省份飞行图(依据广东是人口流动最大省份、湖北是第一个封城的省份); ③ 疫情前后湖北各城市人口流动情况;
import pandas as pd
from pyecharts.charts import *
import pyecharts.options as opts
df1 = pd.read_csv('/XXXXXX/2019-11-30.csv')
df1_1 = df1.groupby(['startProvince']).realIdx.sum().round(1)
df1_2 = df1.groupby(['endProvince']).realIdx.sum().round(1)
df1_3 = pd.merge(df1_1,df1_2,how='left',left_index=True,right_index=True).sort_values('realIdx_x',ascending=False)
df2_1 = df1.groupby(['startProvince','endProvince']).realIdx.sum().round(1).reset_index()
df2_1['r'] = df2_1.groupby(['startProvince'])['realIdx'].rank(method='first',ascending=False)
ls1_city = list(df2_1.query("startProvince in ['广东'] & startProvince!=endProvince & \
realIdx>0 & r<=6").set_index(['startProvince','endProvince']).index)
ls2_city = list(df2_1.query("startProvince in ['湖北'] & startProvince!=endProvince & \
realIdx>0 & r<=6").set_index(['startProvince','endProvince']).index)
df2 = pd.read_csv('/XXXXXX/2020-02-20.csv')
df3_1 = df2.groupby(['startProvince']).realIdx.sum().round(1)
df3_2 = df2.groupby(['endProvince']).realIdx.sum().round(1)
df3_3 = pd.merge(df3_1,df3_2,how='left',left_index=True,right_index=True).sort_values('realIdx_x',ascending=False)
df4_1 = df2.groupby(['startProvince','endProvince']).realIdx.sum().round(1).reset_index()
df4_1['r'] = df4_1.groupby(['startProvince'])['realIdx'].rank(method='first',ascending=False)
ls3_city = list(df4_1.query("startProvince in ['广东'] & startProvince!=endProvince & \
realIdx>0 & r<=6").set_index(['startProvince','endProvince']).index)
ls4_city = list(df4_1.query("startProvince in ['湖北'] & startProvince!=endProvince & \
realIdx>0 & r<=6").set_index(['startProvince','endProvince']).index)
hb1 = df1[df1.startProvince=='湖北'].groupby(['startCity']).realIdx.sum()
hb2 = df2[df2.startProvince=='湖北'].groupby(['startCity']).realIdx.sum()
hb = pd.merge(hb1,hb2,left_index=True,right_index=True)
hb['cha'] = hb.realIdx_x-hb.realIdx_y
hb.sort_values('cha',ascending=False,inplace=True)
2.2 画图
p1 = Geo()
p1.add_schema(maptype='china',is_roam=False)
p1.add('出发地',list(df1_1.to_dict().items()),type_='effectScatter',symbol_size=5,
label_opts=opts.LabelOpts(is_show=False))
p1.add('目的地',list(df1_2.to_dict().items()),type_='heatmap',
is_large=True,blur_size=16,point_size=16)
p1.add('从广东出发的top5',ls1_city,type_='lines',symbol_size=5,label_opts=opts.LabelOpts(is_show=False))
p1.add('从湖北出发的top5',ls2_city,type_='lines',symbol_size=5,label_opts=opts.LabelOpts(is_show=False))
p1.set_global_opts(title_opts=opts.TitleOpts('疫情前'),
visualmap_opts=opts.VisualMapOpts(range_text=['realIdx分组'],is_piecewise=True,
pieces=[{'min':180,'color':'#080177'},
{'min':93,'max':180,'color':'#1203B3'},
{'min':52,'max':93,'color':'#2106fa'},
{'min':36,'max':52,'color':'#7C69FD'},
{'min':0,'max':36,'color':'#D4CDFE'}]))
p2 = Bar()
p2.add_xaxis(list(df1_3.to_dict()['realIdx_x'].keys()))
p2.add_yaxis('出发地',list(df1_3.to_dict()['realIdx_x'].values()),label_opts=opts.LabelOpts(is_show=False))
p2.add_yaxis('目的地',list(df1_3.to_dict()['realIdx_y'].values()),label_opts=opts.LabelOpts(is_show=False))
p2.set_global_opts(title_opts=opts.TitleOpts('2019-11-30各省份人口流动指数'),
legend_opts=opts.LegendOpts(pos_right='right'),datazoom_opts=opts.DataZoomOpts())
p3 = Bar()
p3.add_xaxis(list(df3_3.to_dict()['realIdx_x'].keys()))
p3.add_yaxis('出发地',list(df3_3.to_dict()['realIdx_x'].values()),label_opts=opts.LabelOpts(is_show=False))
p3.add_yaxis('目的地',list(df3_3.to_dict()['realIdx_y'].values()),label_opts=opts.LabelOpts(is_show=False))
p3.set_global_opts(title_opts=opts.TitleOpts('2020-02-20各省份人口流动指数'),
legend_opts=opts.LegendOpts(pos_right='right'),datazoom_opts=opts.DataZoomOpts())
p4 = Geo()
p4.add_schema(maptype='china',is_roam=False)
p4.add('出发地',list(df3_1.to_dict().items()),type_='effectScatter',symbol_size=5,
label_opts=opts.LabelOpts(is_show=False))
p4.add('目的地',list(df3_2.to_dict().items()),type_='heatmap',
is_large=True,blur_size=16,point_size=16)
p4.add('从广东出发的top5',ls3_city,type_='lines',symbol_size=5,label_opts=opts.LabelOpts(is_show=False))
p4.add('从湖北出发的top5',ls4_city,type_='lines',symbol_size=5,label_opts=opts.LabelOpts(is_show=False))
p4.set_global_opts(title_opts=opts.TitleOpts('疫情后'),
visualmap_opts=opts.VisualMapOpts(range_text=['realIdx分组'],is_piecewise=True,
pieces=[{'min':45,'color':'#080177'},
{'min':20,'max':45,'color':'#1203B3'},
{'min':8,'max':20,'color':'#2106fa'},
{'min':4,'max':8,'color':'#7C69FD'},
{'min':0,'max':4,'color':'#D4CDFE'}]))
b = Bar()
b.add_xaxis(list(hb.index))
b.add_yaxis('疫情前',list(hb.realIdx_x.round(1)),label_opts=opts.LabelOpts(is_show=False))
b.add_yaxis('疫情后',list(hb.realIdx_y.round(1)),label_opts=opts.LabelOpts(is_show=False))
b.extend_axis(yaxis=opts.AxisOpts(name='cha',position='right'))
b.extend_axis(yaxis=opts.AxisOpts(name='realIdx',position='left'))
l = Line()
l.add_xaxis(list(hb.index))
l.add_yaxis('cha',list(hb.cha.round(1)),yaxis_index=1,label_opts=opts.LabelOpts(is_show=False))
b.overlap(l)
b.set_global_opts(title_opts=opts.TitleOpts('疫情前后,湖北各城市人口流动情况',pos_left='center'),
legend_opts=opts.LegendOpts(pos_top='10%',pos_right='20%'))
p = Page(layout=Page.DraggablePageLayout)
p.add(p2,p1,p3,p4,b)
p.render('hh.html')
p.render_notebook()
2.3 展示
p.save_resize_html(source='hh.html',
cfg_file='/xxxxxx/chart_config.json',
dest='/xxxxxx/chart.html')
三、可视化大屏
备注:read_json
''' 市.json:可从原数据源下载 '''
a = pd.read_json('/XXXXXX/市.json',orient='index')
ls = []
for i in range(len(a.loc['features',0])):
m = a.loc['features',0][i]['attributes']
ls.append((m['省'],m['省代码'],m['市'],m['市代码']))
pd.DataFrame(ls).head()
谢谢大家 🌹
|