學習一下網上很火的動態條形圖。
學習自: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