动态条形图bar chart race

学习一下网上很火的动态条形图。

学习自:https://pypi.org/project/bar-chart-race/

https://github.com/dexplo/bar_chart_race

https://www.cnblogs.com/traditional/p/12243307.html

数据来自百度指数:https://index.baidu.com/v2/main/index.html#/trend/孙俪?words=孙俪

数据获取方法:https://github.com/longxiaofei/spider-BaiduIndex

数据包(uft-8编码):http://dingdangsunny.cn/wp-content/uploads/2022/05/zhenhuan.csv

时间范围2011.1.1~2022.5.1

绘图代码:

import pandas as pd
import bar_chart_race as bcr
df = pd.read_csv('zhenhuan.csv', index_col=["date"],encoding='utf-8')
df.index = pd.to_datetime(df.index)

df['year'] = df.index.year
df['month'] = df.index.month

# 每月平均
df_m = df.groupby(['year','month']).mean()

df_m.index = pd.to_datetime([str(df_m.index[i][0])+
                             '-'+str(df_m.index[i][1])+'-01' 
 for i in range(0,len(df_m.index))])

# 生成 GIF 图像
bcr.bar_chart_race(
    df=df_m,
    filename='zhenhuan.mp4',
    orientation='h',
    sort='desc',
    n_bars=8,
    fixed_order=False,
    steps_per_period=10,
    interpolate_period=False,
    bar_size=.95,
    period_fmt='%B, %Y',
    perpendicular_bar_func='median',
    period_length=500,
    figsize=(8, 4.5),
    dpi=144,
    title='甄嬛传嫔妃演员热度',
    shared_fontdict={'family':'sans-serif','sans-serif' : 'SimHei'},
    )  

 效果见:https://www.bilibili.com/video/bv1MY4y1t7Pw

posted @ 2022-05-15 08:24  叮叮当当sunny  阅读(432)  评论(1编辑  收藏  举报