大家好,我是欧K。
上期给大家介绍了【中国历届夏季奥运会奖牌数据动态树图】,本期给大家动态展示本次东京奥运会各国的奖牌数据情况,希望对你有所帮助。
先看看效果:
金牌榜中国持续占据榜首,能否完美收官就看今天啦!
接下来我们分三步制作这幅动态展示图:
1. 导入模块
i
mport pandas as pd from pyecharts.charts import Timeline, Line from pyecharts import options as opts from pyecharts.commons.utils import JsCode from pyecharts.globals import ThemeType
升级 pyecharts 包:
可视化部分需要用到 pyecharts==1.9.0,已安装其他低版本的需要升级,如果未安装过pyecharts,直接pip安装就是最新版本
2. Pandas数据处理
2.1 读取数据
df = pd.read_csv('2020东京奥运会奖牌数据.csv', index_col=0, encoding = 'gb18030') df
结果:
数据截止到7日,一共445条数据。
2.2 查看中国每日数据
df1 = df[df['国家']=='中国'] df1
效果:
以上是每日获得奖牌(金牌、银牌、铜牌)数量。
2.3 统计中国、美国、日本、澳大利亚等国家奖牌数据
代码:
all_country_data = [] flg = {} cols = ['国家'] for country in countrys: df1 = df[df['国家']==country] df_t = df1.copy() df2 = df.loc[df['国家']==country,['金牌','银牌','铜牌','总计']] if len(df2.index.tolist()) >= len(cols): cols += df2.index.tolist() flg[country] = df1.iloc[:1, -1].values[0] one_country_data = [country] datasss = [] for i in range(df2.shape[0]): datasss.append(df2[:i+1].apply(lambda x:x.sum()).values.tolist()) d1 = pd.DataFrame(data=datasss, columns=['金牌','银牌','铜牌','总计']) for col in d1.columns: df_t[col] = d1[col].values df_t1 = df_t.loc[:,['金牌']] one_country_data += df_t['金牌'].values.tolist() all_country_data.append(one_country_data) d2 = pd.DataFrame(data=all_country_data,columns=cols) d2 = d2.fillna(method = 'ffill',axis=1)
结果:
这样绘图所需要的数据就准备好了。
3. Pyecharts绘图
3.1 基础折线图
代码:
x_data=cols CHN = [] for d_time in cols: CHN.append(d2[d_time][d2['国家']=='中国'].values.tolist()[0]) line = ( Line() .add_xaxis(x_data) # 中国线条 .add_yaxis( '中国', CHN, label_opts=opts.LabelOpts(is_show=True)) .set_global_opts( title_opts=opts.TitleOpts( title='中国金牌', pos_left='center', ), yaxis_opts=opts.AxisOpts( name='金牌/枚', is_scale=True, max_=40), legend_opts=opts.LegendOpts(is_show=False), )) line.render_notebook()
效果:
3.2 加载样式
代码:
timeline = Timeline(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js), width='980px',height='600px')) timeline.add_schema(is_auto_play=True, is_loop_play=True, is_timeline_show=True, play_interval=500) x_data=cols CHN, USA, JPN, AUS = [], [], [], [] for d_time in cols: CHN.append(d2[d_time][d2['国家']=='中国'].values.tolist()[0]) line = ( Line(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js), width='980px',height='600px')) .add_xaxis(x_data) # 中国线条 .add_yaxis( '中国', CHN, symbol_size=10, is_smooth=True, label_opts=opts.LabelOpts(is_show=True), markpoint_opts=opts.MarkPointOpts( data=[ opts.MarkPointItem( name="", type_='max', value_index=0, symbol='image://'+ flg['中国'], symbol_size=[40, 25], )], label_opts=opts.LabelOpts(is_show=False), ) ) .set_series_opts(linestyle_opts=linestyle_dic,label_opts=opts.LabelOpts(font_size=12, color='red' )) .set_global_opts( title_opts=opts.TitleOpts( title='中国金牌', pos_left='center', pos_top='2%', title_textstyle_opts=opts.TextStyleOpts( color='#DC143C', font_size=20) ), xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(font_size=14, color='red'), axisline_opts=opts.AxisLineOpts(is_show=True, linestyle_opts=opts.LineStyleOpts(width=2, color='#DB7093'))), yaxis_opts=opts.AxisOpts( name='金牌/枚', is_scale=True, max_=40, name_textstyle_opts=opts.TextStyleOpts(font_size=16,font_weight='bold',color='#FFD700'), axislabel_opts=opts.LabelOpts(font_size=13,color='red'), splitline_opts=opts.SplitLineOpts(is_show=True, linestyle_opts=opts.LineStyleOpts(type_='dashed')), axisline_opts=opts.AxisLineOpts(is_show=True, linestyle_opts=opts.LineStyleOpts(width=2, color='#DB7093')) ), legend_opts=opts.LegendOpts(is_show=False, pos_right='1.5%', pos_top='2%', legend_icon='roundRect',orient = 'horizontal'), )) line.render_notebook()
效果:
3.3 动态展示中国每日金牌数据
效果:
3.4 添加其他国家数据
效果:
代码比较多,篇幅原因,部分代码未展示,如果需要可以在下面链接Fork获取(可在线运行):
https://www.heywhale.com/mw/project/610e2d90fe727700176baac3
完。
END
以上就是本期为大家整理的全部内容了,赶快练习起来吧,喜欢的朋友可以点赞、点在看也可以分享让更多人知道