数据可视化:Python+Plotly绘制新冠疫情走势图(三)

第二篇尝试了采用函数展示多个国家/地区的对比图,本篇则开始尝试绘制动态图,如确诊人数随时间变化的动态效果图,同时也采用函数支持多个国家/地区的动态对比。
第一篇:数据可视化:Python+Plotly绘制新冠疫情走势图(一)
第二篇:数据可视化:Python+Plotly绘制新冠疫情走势图(二)

对比各个国家的疫情数据(动态)

基本数据处理

#首先需要剔除中国各省市的数据,只保留国家整体的数据
missing = df.loc[(df['provinceName']!='中国')&(df['countryName']=='中国')]#剔除provincename不是中国且countryname是中国的行,即所有省份的数据
df1 = df.drop(index=missing.index,inplace=False)#删除掉这些行,并把结果保存到df1中
#按照国家-时间删除两项都重复的行
df_wd = df1.sort_values(by='updateTime',ascending=False).drop_duplicates(subset=['countryName','时间'],inplace=False)
#删除continentname为空的行
df_wd.dropna(subset=['continentName'],inplace=True)
#按时间升序排列
df_wd = df_wd.sort_values(by='时间',inplace=False)

定义函数,确认时间段及需对比的国家

def df_merge(*x,date_start,date_end='2020-04-04'):
    df_cry = pd.DataFrame(columns=('provinceName','province_deadCount','province_confirmedCount','时间'))
    df_cry[['province_deadCount','province_confirmedCount']]=df_cry[['province_deadCount','province_confirmedCount']].astype('int')
    for i in x:
        temp = df_wd.loc[(df_wd['provinceName']==i)&(df_wd['时间']>= date_start)&(df_wd['时间']<= date_end)][['provinceName','province_deadCount','province_confirmedCount','时间']]
        df_cry=df_cry.append(temp)
    return df_cry

绘制图像

df_cry = df_merge('意大利','美国','德国','日本',date_start='2020-03-01')
fig = px.scatter(df_cry,x='province_deadCount',y="province_confirmedCount", animation_frame="时间", 
       animation_group=df_cry['provinceName'],color = 'provinceName',
       hover_name=df_cry['provinceName'],
           size="province_confirmedCount",
           title = '各个国家/地区累计确诊人数-死亡人数走势图',
           size_max=45,
           log_y=True,
           log_x=True,
           width=None,
           height= 700)
py.offline.plot(fig,filename="/covid/各个国家(地区)累计确诊人数-死亡人数走势图.html")#路径隐去,可将该html文件保存到本地
多个国家动态图像截图

对比各个国家的疫情数据(动态)

定义函数,确认时间段及需对比的城市

#默认结束日期是最近
def df_merge_city(*x,date_start,date_end='2020-04-04'):
    df_city = pd.DataFrame(columns=('cityName','city_deadCount','city_confirmedCount','时间'))
    df_city[['city_deadCount','city_confirmedCount']]=df_city[['city_deadCount','city_confirmedCount']].astype('int')
    for i in x:
        df_1 = df.loc[(df['cityName']== i)&(df['时间']>= date_start)&(df['时间']<= date_end)].sort_values(by='updateTime',ascending=False).drop_duplicates(subset='时间',inplace=False)
        df_1.sort_values(by='时间',inplace=True)
        temp = df_1[['cityName','city_deadCount','city_confirmedCount','时间']]
        df_city=df_city.append(temp)
    return df_city

绘图

df_city = df_merge_city('武汉','黄冈',date_start='2020-01-24',date_end='2020-03-24')
px.scatter(df_city,x='city_deadCount',y="city_confirmedCount", animation_frame="时间", 
       animation_group=df_city['cityName'],color = 'cityName',
       hover_name=df_city['cityName'],
           size="city_confirmedCount",
           title = '各个国家/地区累计确诊人数-死亡人数走势图',
           size_max=45,
           log_y=True,
           log_x=True,
           width=None,
           height= 700)
py.offline.plot(fig,filename="/covid/各个国家(地区)累计确诊人数-死亡人数走势图.html")#路径隐去,可将该html文件保存到本地
武汉-黄冈对比图

对比各大洲的疫情数据(动态)

#使用之前用到的df_wd数据表,限制时间在3.1之后
df_wd_date = df_wd.loc[df_wd['时间']>='2020-03-01']
#画图
fig = px.scatter(df_wd_date,x='province_deadCount',y="province_confirmedCount", animation_frame="时间", 
           animation_group=df_wd_date['countryName'],color = 'continentName',
           hover_name=df_wd_date['countryName'], facet_col="continentName",
#设置大洲的展示顺序
           category_orders = {'continentName':['亚洲', '欧洲', '北美洲','南美洲', '非洲','大洋洲','其他']}, 
#设置横轴的展示标签
           labels = {'province_deadCount':'死亡人数','province_confirmedCount':'累计确诊人数',
                     'continentName':'大洲'}, 
           size="province_confirmedCount",
           log_y=True, 
           log_x=True,
           title = '各大洲各个国家累计确诊人数-死亡人数走势图',
           size_max=60,
           width=None,
           height= 700)
py.offline.plot(fig,filename="/covid/各大洲累计确诊人数-死亡人数走势图.html")
各大洲累计确诊人数-死亡人数走势图

你可能感兴趣的:(数据可视化:Python+Plotly绘制新冠疫情走势图(三))