栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

爬虫+数据分析,制作一个世界疫情人数增长动态柱状竞赛图2

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

爬虫+数据分析,制作一个世界疫情人数增长动态柱状竞赛图2

有了上面的数据,直接读进来。

allData = pd.read_pickle(os.path.join(data_dir, "allData疫情数据"))

然后,提取日期,congfirm, 

组成dataframe

def make_data(allData):
    col = allData.keys()
    dct_coutry = {}
    for c in col:
        one_country = allData[c]
        list = one_country['list']
        /confirm/i_list = []
        date_list = []
        for l in list:
            cfm = l['total']['/confirm/i']
            /confirm/i_list.append(cfm)
            dt = l['date']
            date_list.append(dt)
        ser_one = pd.Series( /confirm/i_list,index=date_list)
        dct_coutry[c] = ser_one
    return dct_coutry

cov19 = make_data(allData)

因为是每一天的,所以只用每周1的数据作图。

def contstruct_cov(cov19):
    df = pd.Dataframe(cov19)
    df.columns = ['china', 'usa', 'uk', 'iran', 'spain', 'korea', 'germey', 'italy', 'india']
    print(df.shape)
    df = df.dropna(axis=0)
    print("after drop NA : ", df.shape)
    df.index = pd.to_datetime(df.index)
    print("after make index : ", df.index)
    df['dayofweek'] = df.index.dayofweek
    df_Monday = df[df['dayofweek'] == 1]
    print("Monday data shape: ", df_Monday.shape)
    df_Monday = df_Monday[['china', 'usa', 'uk', 'germey', 'italy',  'india']]
    print(df_Monday.shape)
    return df_Monday
df_Monday = contstruct_cov(cov19)
df_Monday

然后,把数据变成长数据形式

def my_reshapeData(df_Monday):
    df = df_Monday.stack()
    df = df.reset_index()
    df.columns = ['date', 'name', 'value' ]
    return df

df_final = my_reshapeData(df_Monday)

df_final
colors = dict(zip([ 'china', 'usa', 'uk', 'germey', 'italy', 'india'],
    ["#adb0ff", "#ffb3ff", "#90d595", "#e48381", "#aafbff", "#f7bb5f"] ))

接下来就作图了

number = list(range(82))*6
number.sort()
number
df['number'] = number

df.head()

def draw_barchart(number):
    dff = df[df['number'] == number].sort_values(by='value', ascending = True)
    ax.clear()
    ax.barh(dff['name'], dff['value'], color = [ colors[x] for x in dff['name']])

fig, ax = plt.subplots(figsize = (15, 8))
animator = animation.FuncAnimation(fig, draw_barchart, frames = range(0, 17))
HTML(animator.to_jshtml())
HTML(animator.save(os.path.join(data_dir, "race_bar.gif")))

只做了个简单的,如果设置具体的再参考其他网站

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/294029.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号