小福利,应用python的raceplot模块制作动态可视化图表
源数据
import json,folium,requests
import mimetypes
import http.client
import pandas as pd
from folium.plugins import HeatMap
from pandas.io.json import json_normalize
import plotly.express as px
from raceplotly.plots import barplot
import pycountry
df=pd.read_excel('D:\stockdata\疫情数据.xlsx')
df_confirm=df.groupby('国家/地区').agg('sum')
date_list=list(df_confirm.columns)
def get_country_code(name):
try:
return pycountry.countries.lookup(name).alpha_3
except:
return None
df_confirm['国家/地区']=df_confirm.index
df_confirm['iso_alpha_3']=df_confirm['国家/地区'].apply(get_country_code)
df_long = pd.melt(df_confirm, id_vars=['国家/地区','iso_alpha_3'],value_vars= date_list)
df_long.to_excel('100.xlsx')
df_long=df_long.rename(columns={'variable':'日期','value':'人数'})
# df_long['日期']=pd.to_datetime(df_long['日期'],format='%Y-%m-%d')
df_long.head(160)
df_long2=df_long
# df_long2.iloc[2000:2050,:]
def dataconvert(dt):
dt=str(dt).strip()
list1=dt.split('/')
part1=list1[2]
if len(part1) == 1:
part1 ='200' + str(part1)
elif len(part1) == 2:
part1 = '20' + str(part1)
part2 = list1[0]
if len(part2) == 1:
part2 = 'O' + str(part2)
elif len(part2) == 4:
part2 = str(part2)[-2:]
return part1 + '/' + part2 + '/' + list1[1]
# df_long2.日期=df_long2.日期.apply(dataconvert)
# df_long2.日期=pd.to_datetime(df_long2.日期).dt.date
df_long2.sort_values(by=['日期'])
my_raceplot = barplot(df_long2,
item_column= '国家/地区',
value_column= '人数',
time_column='日期',
top_entries=5)
fig2=my_raceplot.plot(title = '前5名国家2020/1 to 2021/1',
item_label = '前5名国家',
value_label = '确诊案例',
frame_duration = 50)
fig2
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)