【Python視覺化】使用Pyecharts進行奧運會視覺化分析~

AwesomeTang發表於2020-04-29

image


專案全部程式碼 & 資料集都可以訪問我的KLab --【Pyecharts】奧運會資料集視覺化分析~獲取,點選Fork即可~


  • 受疫情影響,2020東京奧運會將延期至2021年舉行;

  • 雖然延期,但此次奧運會依舊會沿用「2020東京奧運會」這個名稱;

  • 這也將是奧運會歷史上首次延期(1916年、1940年、1944年曾因一戰,二戰停辦);

既然奧運會延期了,那我們就來回顧下整個奧運會的歷史吧??~


本專案將會從以下角度來呈現奧運會歷史:

  1. ?各國累計獎牌數;

  2. ⚽️各項運動產生金牌數

  3. ⛳️運動員層面

    • 參賽人數趨勢

    • 女性參賽比例趨勢

    • 獲得金牌最多的運動員

    • 獲得獎牌/金牌比例

    • 各專案運動員平均體質資料

  4. 主要國家表現

    • ??中國表現

    • ??美國表現

  5. ?被單個國家統治的奧運會專案

匯入庫 & 資料

import pandas as pd
import numpy as np
import pyecharts
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode
athlete_data = pd.read_csv('/home/kesci/input/olympic/athlete_events.csv')
noc_region = pd.read_csv('/home/kesci/input/olympic/noc_regions.csv')

# 關聯代表國家
data = pd.merge(athlete_data, noc_region, on='NOC', how='left')
data.head()

資料示例

累計獎牌數

夏季奧運會 & 冬季奧運會分別統計

  • ?️夏季奧運會開始於1896年雅典奧運會;

  • ❄️冬季奧運會開始於1924年慕尼黑冬奧會;

medal_data = data.groupby(['Year', 'Season', 'region', 
                                        'Medal'])['Event'].nunique().reset_index()
medal_data.columns = ['Year', 'Season', 'region', 'Medal', 'Nums']                                      
medal_data = medal_data.sort_values(by="Year" , ascending=True) 

medal_data = data.groupby(['Year', 'Season', 'region', 
                                        'Medal'])['Event'].nunique().reset_index()
medal_data.columns = ['Year', 'Season', 'region', 'Medal', 'Nums']                                      
medal_data = medal_data.sort_values(by="Year" , ascending=True) 

各國夏奧會累計獎牌數

  • 截止2016年夏季奧運會,美俄分別獲得了2544和1577枚獎牌,位列一二位;

  • 中國由於參加奧運會時間較晚,截止2016年累計獲得了545枚獎牌,位列第七位;

year_list = sorted(list(set(medal_data['Year'].to_list())), reverse=True)

tl = Timeline(init_opts=opts.InitOpts(theme='dark', width='1000px', height='1000px'))
tl.add_schema(is_timeline_show=True,is_rewind_play=True, is_inverse=False,
             label_opts=opts.LabelOpts(is_show=False))

for year in year_list:
    t_data = medal_stat(year)[::-1]
    bar = (
        Bar(init_opts=opts.InitOpts())
            .add_xaxis([x[0] for x in t_data])
           .add_yaxis("銅牌?", [x[3] for x in t_data], 
                        stack='stack1',
                        itemstyle_opts=opts.ItemStyleOpts(border_color='rgb(220,220,220)',color='rgb(218,165,32)'))
            .add_yaxis("銀牌?", [x[2] for x in t_data], 
                        stack='stack1',
                        itemstyle_opts=opts.ItemStyleOpts(border_color='rgb(220,220,220)',color='rgb(192,192,192)'))
            .add_yaxis("金牌?️", [x[1] for x in t_data], 
                        stack='stack1',
                        itemstyle_opts=opts.ItemStyleOpts(border_color='rgb(220,220,220)',color='rgb(255,215,0)'))
            .set_series_opts(label_opts=opts.LabelOpts(is_show=True, 
                                                       position='insideRight',
                                                       font_style='italic'),)
            .set_global_opts(
                title_opts=opts.TitleOpts(title="各國累計獎牌數(夏季奧運會)"),
                xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
                legend_opts=opts.LegendOpts(is_show=True),
                graphic_opts=[opts.GraphicGroup(graphic_item=opts.GraphicItem(
                                                   rotation=JsCode("Math.PI / 4"),
                                                   bounding="raw",
                                                   right=110,
                                                   bottom=110,
                                                   z=100),
                                               children=[
                                                   opts.GraphicRect(
                                                       graphic_item=opts.GraphicItem(
                                                           left="center", top="center", z=100
                                                       ),
                                                       graphic_shape_opts=opts.GraphicShapeOpts(
                                                           width=400, height=50
                                                       ),
                                                       graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
                                                           fill="rgba(0,0,0,0.3)"
                                                       ),
                                                   ),
                                                   opts.GraphicText(
                                                       graphic_item=opts.GraphicItem(
                                                           left="center", top="center", z=100
                                                       ),
                                                       graphic_textstyle_opts=opts.GraphicTextStyleOpts(
                                                           text=year,
                                                           font="bold 26px Microsoft YaHei",
                                                           graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
                                                               fill="#fff"
                                                           ),
                                                       ),
                                                   ),
                                               ],
                                            )
                                    ],)
        .reversal_axis())
    tl.add(bar, year)

tl.render_notebook()

各國冬奧會累計獎牌數

year_list = sorted(list(set(medal_data['Year'][medal_data.Season=='Winter'].to_list())), reverse=True)

tl = Timeline(init_opts=opts.InitOpts(theme='dark', width='1000px', height='1000px'))
tl.add_schema(is_timeline_show=True,is_rewind_play=True, is_inverse=False,
             label_opts=opts.LabelOpts(is_show=False))

for year in year_list:
    t_data = medal_stat(year, 'Winter')[::-1]
    bar = (
        Bar(init_opts=opts.InitOpts(theme='dark'))
            .add_xaxis([x[0] for x in t_data])
            .add_yaxis("銅牌?", [x[3] for x in t_data], 
                        stack='stack1',
                        itemstyle_opts=opts.ItemStyleOpts(border_color='rgb(220,220,220)',color='rgb(218,165,32)'))
            .add_yaxis("銀牌?", [x[2] for x in t_data], 
                        stack='stack1',
                        itemstyle_opts=opts.ItemStyleOpts(border_color='rgb(220,220,220)',color='rgb(192,192,192)'))
            .add_yaxis("金牌?️", [x[1] for x in t_data], 
                        stack='stack1',
                        itemstyle_opts=opts.ItemStyleOpts(border_color='rgb(220,220,220)',color='rgb(255,215,0)'))
            .set_series_opts(label_opts=opts.LabelOpts(is_show=True, 
                                                       position='insideRight',
                                                       font_style='italic'),)
            .set_global_opts(
                title_opts=opts.TitleOpts(title="各國累計獎牌數(冬季奧運會)"),
                xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
                legend_opts=opts.LegendOpts(is_show=True),
                graphic_opts=[opts.GraphicGroup(graphic_item=opts.GraphicItem(
                                                   rotation=JsCode("Math.PI / 4"),
                                                   bounding="raw",
                                                   right=110,
                                                   bottom=110,
                                                   z=100),
                                               children=[
                                                   opts.GraphicRect(
                                                       graphic_item=opts.GraphicItem(
                                                           left="center", top="center", z=100
                                                       ),
                                                       graphic_shape_opts=opts.GraphicShapeOpts(
                                                           width=400, height=50
                                                       ),
                                                       graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
                                                           fill="rgba(0,0,0,0.3)"
                                                       ),
                                                   ),
                                                   opts.GraphicText(
                                                       graphic_item=opts.GraphicItem(
                                                           left="center", top="center", z=100
                                                       ),
                                                       graphic_textstyle_opts=opts.GraphicTextStyleOpts(
                                                           text='截止{}'.format(year),
                                                           font="bold 26px Microsoft YaHei",
                                                           graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
                                                               fill="#fff"
                                                           ),
                                                       ),
                                                   ),
                                               ],
                                            )
                                    ],)
            .reversal_axis())
    tl.add(bar, year)

tl.render_notebook()

各項運動產生金牌數

基於2016年夏奧會和2014年冬奧會統計;

  • ?田徑 & 游泳是大項,在2016年夏奧會上分別產生了47和34枚金牌;
background_color_js = """new echarts.graphic.RadialGradient(0.5, 0.5, 1, [{
                                        offset: 0,
                                        color: '#696969'
                                    }, {
                                        offset: 1,
                                        color: '#000000'
                                    }])"""

tab = Tab()
temp = data[(data['Medal']=='Gold') & (data['Year']==2016) & (data['Season']=='Summer')]

event_medal = temp.groupby(['Sport'])['Event'].nunique().reset_index()
event_medal.columns = ['Sport', 'Nums']                                      
event_medal = event_medal.sort_values(by="Nums" , ascending=False) 


pie = (Pie(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js), width='1000px', height='800px'))
       .add('金牌?️', [(row['Sport'], row['Nums']) for _, row in event_medal.iterrows()],
            radius=["30%", "75%"],
            rosetype="radius")
       .set_global_opts(title_opts=opts.TitleOpts(title="2016年夏季奧運會各項運動產生金牌佔比", 
                                                  pos_left="center",
                                                  title_textstyle_opts=opts.TextStyleOpts(color="white", font_size=20),     ),
                        legend_opts=opts.LegendOpts(is_show=False))
       .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {d}%"),
                        tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"),)
      )
tab.add(pie, '2016年夏奧會')

temp = data[(data['Medal']=='Gold') & (data['Year']==2014) & (data['Season']=='Winter')]

event_medal = temp.groupby(['Sport'])['Event'].nunique().reset_index()
event_medal.columns = ['Sport', 'Nums']                                      
event_medal = event_medal.sort_values(by="Nums" , ascending=False) 


pie = (Pie(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js), width='1000px', height='800px'))
       .add('金牌?️', [(row['Sport'], row['Nums']) for _, row in event_medal.iterrows()],
            radius=["30%", "75%"],
            rosetype="radius")
       .set_global_opts(title_opts=opts.TitleOpts(title="2014年冬季奧運會各項運動產生金牌佔比", 
                                                  pos_left="center",
                                                  title_textstyle_opts=opts.TextStyleOpts(color="white", font_size=20),     ),
                        legend_opts=opts.LegendOpts(is_show=False))
       .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {d}%"),
                        tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
        ),)
      )
tab.add(pie, '2014年冬奧會')
tab.render_notebook()

運動員層面

歷年參賽人數趨勢

  • 從人數來看,每屆夏奧會參賽人數都是冬奧會的4-5倍;

  • 整體參賽人數是上漲趨勢,但由於歷史原因也出現過波動,如1980年莫斯科奧運會層遭遇65個國家抵制

athlete = data.groupby(['Year', 'Season'])['Name'].nunique().reset_index()
athlete.columns = ['Year', 'Season', 'Nums']                                      
athlete = athlete.sort_values(by="Year" , ascending=True) 

x_list, y1_list, y2_list = [], [], []

for _, row in athlete.iterrows():
    x_list.append(str(row['Year']))
    if row['Season'] == 'Summer':
        y1_list.append(row['Nums'])
        y2_list.append(None)
    else:
        y2_list.append(row['Nums'])
        y1_list.append(None)

background_color_js = (
    "new echarts.graphic.LinearGradient(1, 1, 0, 0, "
    "[{offset: 0, color: '#008B8B'}, {offset: 1, color: '#FF6347'}], false)"
)

       
line = (
    Line(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js), width='1000px', height='600px'))
    .add_xaxis(x_list)
    .add_yaxis("夏季奧運會", 
        y1_list, 
        is_smooth=True, 
        is_connect_nones=True,
        symbol="circle",
        symbol_size=6,
        linestyle_opts=opts.LineStyleOpts(color="#fff"),
        label_opts=opts.LabelOpts(is_show=False, position="top", color="white"),
        itemstyle_opts=opts.ItemStyleOpts(
            color="green", border_color="#fff", border_width=3),
        tooltip_opts=opts.TooltipOpts(is_show=True))
    .add_yaxis("冬季季奧運會", 
        y2_list, 
        is_smooth=True, 
        is_connect_nones=True, 
        symbol="circle",
        symbol_size=6,
        linestyle_opts=opts.LineStyleOpts(color="#FF4500"),
        label_opts=opts.LabelOpts(is_show=False, position="top", color="white"),
        itemstyle_opts=opts.ItemStyleOpts(
            color="red", border_color="#fff", border_width=3),
        tooltip_opts=opts.TooltipOpts(is_show=True))
    .set_series_opts(
        markarea_opts=opts.MarkAreaOpts(
            label_opts=opts.LabelOpts(is_show=True, position="bottom", color="white"),
            data=[
                opts.MarkAreaItem(name="第一次世界大戰", x=(1914, 1918)),
                opts.MarkAreaItem(name="第二次世界大戰", x=(1939, 1945)),
            ]
        )
    )
    .set_global_opts(title_opts=opts.TitleOpts(title="歷屆奧運會參賽人數",
                                                pos_left="center",
                                                title_textstyle_opts=opts.TextStyleOpts(color="white", font_size=20),),
                     legend_opts=opts.LegendOpts(is_show=True, pos_top='5%',
                                                 textstyle_opts=opts.TextStyleOpts(color="white", font_size=12)),
                     xaxis_opts=opts.AxisOpts(type_="value",
                                                min_=1904,
                                                max_=2016,
                                                boundary_gap=False,
                                                axislabel_opts=opts.LabelOpts(margin=30, color="#ffffff63",
                                                                              formatter=JsCode("""function (value) 
                                                                               {return value+'年';}""")),
                                                axisline_opts=opts.AxisLineOpts(is_show=False),
                                                axistick_opts=opts.AxisTickOpts(
                                                    is_show=True,
                                                    length=25,
                                                    linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
                                                ),
                                                splitline_opts=opts.SplitLineOpts(
                                                    is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
                                                ),
                                            ),
                    yaxis_opts=opts.AxisOpts(
                                            type_="value",
                                            position="right",
                                            axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63"),
                                            axisline_opts=opts.AxisLineOpts(
                                                linestyle_opts=opts.LineStyleOpts(width=2, color="#fff")
                                            ),
                                            axistick_opts=opts.AxisTickOpts(
                                                is_show=True,
                                                length=15,
                                                linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
                                            ),
                                            splitline_opts=opts.SplitLineOpts(
                                                is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
                                            ),
                                        ),)
)

line.render_notebook()

歷年女性運動員佔比趨勢

一開始奧運會基本是「男人的運動」,女性運動員僅為個位數,到近幾屆奧運會男女參賽人數基本趨於相等;

# 歷年男性運動員人數
m_data = data[data.Sex=='M'].groupby(['Year', 'Season'])['Name'].nunique().reset_index()
m_data.columns = ['Year', 'Season', 'M-Nums']                                      
m_data = m_data.sort_values(by="Year" , ascending=True) 

# 歷年女性運動員人數
f_data = data[data.Sex=='F'].groupby(['Year', 'Season'])['Name'].nunique().reset_index()
f_data.columns = ['Year', 'Season', 'F-Nums']                                      
f_data = f_data.sort_values(by="Year" , ascending=True) 

t_data = pd.merge(m_data, f_data, on=['Year', 'Season'])
t_data['F-rate'] = round(t_data['F-Nums'] / (t_data['F-Nums']  + t_data['M-Nums'] ), 4)


x_list, y1_list, y2_list = [], [], []

for _, row in t_data.iterrows():
    x_list.append(str(row['Year']))
    if row['Season'] == 'Summer':
        y1_list.append(row['F-rate'])
        y2_list.append(None)
    else:
        y2_list.append(row['F-rate'])
        y1_list.append(None)

background_color_js = (
    "new echarts.graphic.LinearGradient(0, 0, 0, 1, "
    "[{offset: 0, color: '#008B8B'}, {offset: 1, color: '#FF6347'}], false)"
)

       
line = (
    Line(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js), width='1000px', height='600px'))
    .add_xaxis(x_list)
    .add_yaxis("夏季奧運會", 
        y1_list, 
        is_smooth=True, 
        is_connect_nones=True,
        symbol="circle",
        symbol_size=6,
        linestyle_opts=opts.LineStyleOpts(color="#fff"),
        label_opts=opts.LabelOpts(is_show=False, position="top", color="white"),
        itemstyle_opts=opts.ItemStyleOpts(color="green", border_color="#fff", border_width=3),
        tooltip_opts=opts.TooltipOpts(is_show=True),)
    .add_yaxis("冬季季奧運會", 
        y2_list, 
        is_smooth=True, 
        is_connect_nones=True, 
        symbol="circle",
        symbol_size=6,
        linestyle_opts=opts.LineStyleOpts(color="#FF4500"),
        label_opts=opts.LabelOpts(is_show=False, position="top", color="white"),
        itemstyle_opts=opts.ItemStyleOpts(color="red", border_color="#fff", border_width=3),
        tooltip_opts=opts.TooltipOpts(is_show=True),)
    .set_series_opts(tooltip_opts=opts.TooltipOpts(trigger="item", formatter=JsCode("""function (params) 
                                                                           {return params.data[0]+ '年: ' + Number(params.data[1])*100 +'%';}""")),)
    .set_global_opts(title_opts=opts.TitleOpts(title="歷屆奧運會參賽女性佔比趨勢",
                                                pos_left="center",
                                                title_textstyle_opts=opts.TextStyleOpts(color="white", font_size=20),),
                     legend_opts=opts.LegendOpts(is_show=True, pos_top='5%',
                                                 textstyle_opts=opts.TextStyleOpts(color="white", font_size=12)),
                     xaxis_opts=opts.AxisOpts(type_="value",
                                                min_=1904,
                                                max_=2016,
                                                boundary_gap=False,
                                                axislabel_opts=opts.LabelOpts(margin=30, color="#ffffff63",
                                                                              formatter=JsCode("""function (value) 
                                                                               {return value+'年';}""")),
                                                axisline_opts=opts.AxisLineOpts(is_show=False),
                                                axistick_opts=opts.AxisTickOpts(
                                                    is_show=True,
                                                    length=25,
                                                    linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
                                                ),
                                                splitline_opts=opts.SplitLineOpts(
                                                    is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
                                                ),
                                            ),
                    yaxis_opts=opts.AxisOpts(
                                            type_="value",
                                            position="right",
                                            axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63",
                                                                          formatter=JsCode("""function (value) 
                                                                           {return Number(value *100)+'%';}""")),
                                            axisline_opts=opts.AxisLineOpts(
                                                linestyle_opts=opts.LineStyleOpts(width=2, color="#fff")
                                            ),
                                            axistick_opts=opts.AxisTickOpts(
                                                is_show=True,
                                                length=15,
                                                linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
                                            ),
                                            splitline_opts=opts.SplitLineOpts(
                                                is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
                                            ),
                                        ),)
)

line.render_notebook()

獲得金牌最多的運動員

  • 排在第一的是美國泳壇名將「菲爾普斯」,截止2016年奧運會總共獲得了23枚金牌;

  • 博爾特累計獲得8枚奧運會金牌;

temp = data[(data['Medal']=='Gold')]

athlete = temp.groupby(['Name'])['Medal'].count().reset_index()
athlete.columns = ['Name', 'Nums']                                      
athlete = athlete.sort_values(by="Nums" , ascending=True)


background_color_js = (
    "new echarts.graphic.LinearGradient(0, 0, 1, 1, "
    "[{offset: 0, color: '#008B8B'}, {offset: 1, color: '#FF6347'}], false)"
)

pb = (
    PictorialBar(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js), width='1000px', height='800px'))
    .add_xaxis([x.replace(' ','\n') for x in athlete['Name'].tail(10).tolist()])
    .add_yaxis(
        "",
        athlete['Nums'].tail(10).tolist(),
        label_opts=opts.LabelOpts(is_show=False),
        symbol_size=25,
        symbol_repeat='fixed',
        symbol_offset=[0, 0],
        is_symbol_clip=True,
        symbol='image://https://cdn.kesci.com/upload/image/q8f8otrlfc.png')
    .reversal_axis()
    .set_global_opts(
        title_opts=opts.TitleOpts(title="獲得金牌數量最多的運動員", pos_left='center',
                                  title_textstyle_opts=opts.TextStyleOpts(color="white", font_size=20),),
        xaxis_opts=opts.AxisOpts(is_show=False,),
        yaxis_opts=opts.AxisOpts(
            axistick_opts=opts.AxisTickOpts(is_show=False),
            axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(opacity=0)
            ),
        ),
    ))

pb.render_notebook()

獲得金牌/獎牌比例

看菲爾普斯拿金牌拿到手軟,但實際上想獲得一塊金牌的難度高嗎?

  • 整個奧運會(包括夏季,冬季奧運會)歷史上參賽人數為134732,獲得過金牌的運動員只有10413,佔比7.7%;

  • 獲得過獎牌(包括金銀銅)的運動員有28202人,佔比20.93%;


total_athlete = len(set(data['Name']))
medal_athlete = len(set(data['Name'][data['Medal'].isin(['Gold', 'Silver', 'Bronze'])]))
gold_athlete = len(set(data['Name'][data['Medal']=='Gold']))




l1 = Liquid(init_opts=opts.InitOpts(theme='dark', width='1000px', height='800px'))
l1.add("獲得獎牌", [medal_athlete/total_athlete], 
            center=["70%", "50%"],
            label_opts=opts.LabelOpts(font_size=50,
                formatter=JsCode(
                    """function (param) {
                            return (Math.floor(param.value * 10000) / 100) + '%';
                        }"""),
                position="inside",
            ))
l1.set_global_opts(title_opts=opts.TitleOpts(title="獲得過獎牌比例", pos_left='62%', pos_top='8%'))
l1.set_series_opts(tooltip_opts=opts.TooltipOpts(is_show=False))

l2 = Liquid(init_opts=opts.InitOpts(theme='dark', width='1000px', height='800px'))
l2.add("獲得金牌",
        [gold_athlete/total_athlete],
        center=["25%", "50%"],
        label_opts=opts.LabelOpts(font_size=50,
            formatter=JsCode(
                """function (param) {
                        return (Math.floor(param.value * 10000) / 100) + '%';
                    }"""),
            position="inside",
        ),)
l2.set_global_opts(title_opts=opts.TitleOpts(title="獲得過金牌比例", pos_left='17%', pos_top='8%'))
l2.set_series_opts(tooltip_opts=opts.TooltipOpts(is_show=False))


grid = Grid().add(l1, grid_opts=opts.GridOpts()).add(l2, grid_opts=opts.GridOpts())
grid.render_notebook()

運動員平均體質資料

根據不同的運動專案進行統計

  • 運動員平均身高最高的專案是籃球,女子平均身高達182cm,男子平均身高達到194cm;

  • 在男子專案中,運動員平均體重最大的專案是拔河,平均體重達到96kg(拔河自第七屆奧運會後已取消);

  • 運動員平均年齡最大的專案是Art competition(自行百度這奇怪的專案),平均年齡46歲,除此之外便是馬術和射擊,男子平均年齡分別為34.4歲和34.2歲,女子平均年齡34.22歲和29.12s歲;

tool_js = """function (param) {return param.data[2] +'<br/>' 
            +'平均體重: '+Number(param.data[0]).toFixed(2)+' kg<br/>'
            +'平均身高: '+Number(param.data[1]).toFixed(2)+' cm<br/>'
            +'平均年齡: '+Number(param.data[3]).toFixed(2);}"""

background_color_js = (
    "new echarts.graphic.LinearGradient(1, 0, 0, 1, "
    "[{offset: 0, color: '#008B8B'}, {offset: 1, color: '#FF6347'}], false)"
)


temp_data = data[data['Sex']=='M'].groupby(['Sport'])['Age', 'Height', 'Weight'].mean().reset_index().dropna(how='any')

scatter = (Scatter(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js), width='1000px', height='600px'))
           .add_xaxis(temp_data['Weight'].tolist())
           .add_yaxis("男性", [[row['Height'], row['Sport'], row['Age']] for _, row in temp_data.iterrows()],
                      # 漸變效果實現部分
                      color=JsCode("""new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                                        offset: 0,
                                        color: 'rgb(129, 227, 238)'
                                    }, {
                                        offset: 1,
                                        color: 'rgb(25, 183, 207)'
                                    }])"""))
           .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
           .set_global_opts(
               title_opts=opts.TitleOpts(title="各專案運動員平均升高體重年齡",pos_left="center",
                                         title_textstyle_opts=opts.TextStyleOpts(color="white", font_size=20)),
               legend_opts=opts.LegendOpts(is_show=True, pos_top='5%',
                                           textstyle_opts=opts.TextStyleOpts(color="white", font_size=12)),
               tooltip_opts = opts.TooltipOpts(formatter=JsCode(tool_js)),
               xaxis_opts=opts.AxisOpts(
                   name='體重/kg',
                   # 設定座標軸為數值型別
                   type_="value", 
                   is_scale=True,
                   # 顯示分割線
                   axislabel_opts=opts.LabelOpts(margin=30, color="white"),
                   axisline_opts=opts.AxisLineOpts(is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")),
                   axistick_opts=opts.AxisTickOpts(is_show=True, length=25,
                                                   linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")),
                   splitline_opts=opts.SplitLineOpts(is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
                                                )),
               yaxis_opts=opts.AxisOpts(
                   name='身高/cm',
                   # 設定座標軸為數值型別
                   type_="value",
                   # 預設為False表示起始為0
                   is_scale=True,
                   axislabel_opts=opts.LabelOpts(margin=30, color="white"),
                   axisline_opts=opts.AxisLineOpts(is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")),
                   axistick_opts=opts.AxisTickOpts(is_show=True, length=25,
                                                   linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")),
                   splitline_opts=opts.SplitLineOpts(is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
                                                )),
               visualmap_opts=opts.VisualMapOpts(is_show=False, type_='size', range_size=[5,50], min_=10, max_=40)
    ))

temp_data = data[data['Sex']=='F'].groupby(['Sport'])['Age', 'Height', 'Weight'].mean().reset_index().dropna(how='any')
    
scatter1 = (Scatter()
           .add_xaxis(temp_data['Weight'].tolist())
           .add_yaxis("女性", [[row['Height'], row['Sport'], row['Age']] for _, row in temp_data.iterrows()],
                     itemstyle_opts=opts.ItemStyleOpts(
                         color=JsCode("""new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                                        offset: 0,
                                        color: 'rgb(251, 118, 123)'
                                    }, {
                                        offset: 1,
                                        color: 'rgb(204, 46, 72)'
                                    }])""")))
           .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        )
scatter.overlap(scatter1)
scatter.render_notebook() 

??中國奧運會表現

CN_data = data[data.region=='China']
CN_data.head()

歷屆奧運會參賽人數

background_color_js = (
    "new echarts.graphic.LinearGradient(1, 0, 0, 1, "
    "[{offset: 0, color: '#008B8B'}, {offset: 1, color: '#FF6347'}], false)"
)



athlete = CN_data.groupby(['Year', 'Season'])['Name'].nunique().reset_index()
athlete.columns = ['Year', 'Season', 'Nums']                                      
athlete = athlete.sort_values(by="Year" , ascending=False) 


        
s_bar = (
        Bar(init_opts=opts.InitOpts(theme='dark', width='1000px', height='300px'))
        .add_xaxis([row['Year'] for _, row in athlete[athlete.Season=='Summer'].iterrows()])
        .add_yaxis("參賽人數", [row['Nums'] for _, row in athlete[athlete.Season=='Summer'].iterrows()],
                  category_gap='40%',
                  itemstyle_opts=opts.ItemStyleOpts(
                                border_color='rgb(220,220,220)',
                                color=JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, 
                                             [{
                                                 offset: 1,
                                                 color: '#00BFFF'
                                             }, {
                                                 offset: 0,
                                                 color: '#32CD32'
                                             }])""")))
        .set_series_opts(label_opts=opts.LabelOpts(is_show=True, 
                                                position='top',
                                                font_style='italic'))
        .set_global_opts(
            title_opts=opts.TitleOpts(title="中國曆年奧運會參賽人數-夏奧會", pos_left='center'),
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
            legend_opts=opts.LegendOpts(is_show=False),
            yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63")),
            graphic_opts=[
            opts.GraphicImage(
                graphic_item=opts.GraphicItem(
                    id_="logo", right=0, top=0, z=-10, bounding="raw", origin=[75, 75]
                ),
                graphic_imagestyle_opts=opts.GraphicImageStyleOpts(
                    image="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586619952245&di=981a36305048f93eec791980acc99cf7&imgtype=0&src=http%3A%2F%2Fimg5.mtime.cn%2Fmg%2F2017%2F01%2F06%2F172210.42721559.jpg",
                    width=1000,
                    height=600,
                    opacity=0.6,),
            )
        ],)
        )

        
w_bar = (
        Bar(init_opts=opts.InitOpts(theme='dark',width='1000px', height='300px'))
        .add_xaxis([row['Year'] for _, row in athlete[athlete.Season=='Winter'].iterrows()])
        .add_yaxis("參賽人數", [row['Nums'] for _, row in athlete[athlete.Season=='Winter'].iterrows()],
                  category_gap='50%',
                  itemstyle_opts=opts.ItemStyleOpts(
                                border_color='rgb(220,220,220)',
                                color=JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, 
                                             [{
                                                 offset: 1,
                                                 color: '#00BFFF'
                                             }, {
                                                 offset: 0.8,
                                                 color: '#FFC0CB'
                                             }, {
                                                 offset: 0,
                                                 color: '#40E0D0'
                                             }])""")))
        .set_series_opts(label_opts=opts.LabelOpts(is_show=True, 
                                                position='top',
                                                font_style='italic'))
        .set_global_opts(
            title_opts=opts.TitleOpts(title="中國曆年奧運會參賽人數-冬奧會", pos_left='center'),
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
            legend_opts=opts.LegendOpts(is_show=False),
            yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63")),
            graphic_opts=[
            opts.GraphicImage(
                graphic_item=opts.GraphicItem(
                    id_="logo", right=0, top=-300, z=-10, bounding="raw", origin=[75, 75]
                ),
                graphic_imagestyle_opts=opts.GraphicImageStyleOpts(
                    image="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586619952245&di=981a36305048f93eec791980acc99cf7&imgtype=0&src=http%3A%2F%2Fimg5.mtime.cn%2Fmg%2F2017%2F01%2F06%2F172210.42721559.jpg",
                    width=1000,
                    height=600,
                    opacity=0.6,),
            )
        ],)
        )


page = (
    Page()
    .add(s_bar,)
    .add(w_bar,)
)
page.render_notebook()

歷屆奧運會獎牌數

background_color_js = (
    "new echarts.graphic.LinearGradient(1, 0, 0, 1, "
    "[{offset: 0, color: '#008B8B'}, {offset: 1, color: '#FF6347'}], false)"
)



CN_medals = CN_data.groupby(['Year', 'Season', 'Medal'])['Event'].nunique().reset_index()
CN_medals.columns = ['Year', 'Season', 'Medal', 'Nums']                                      
CN_medals = CN_medals.sort_values(by="Year" , ascending=False) 


        
s_bar = (
        Bar(init_opts=opts.InitOpts(theme='dark', width='1000px', height='300px'))
        .add_xaxis(sorted(list(set([row['Year'] for _, row in CN_medals[CN_medals.Season=='Summer'].iterrows()])), reverse=True))
        .add_yaxis("金牌", [row['Nums'] for _, row in CN_medals[(CN_medals.Season=='Summer') & (CN_medals.Medal=='Gold')].iterrows()],
                  category_gap='20%',
                  itemstyle_opts=opts.ItemStyleOpts(
                                border_color='rgb(220,220,220)',
                                color=JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, 
                                             [{
                                                 offset: 0,
                                                 color: '#FFD700'
                                             }, {
                                                 offset: 1,
                                                 color: '#FFFFF0'
                                             }])""")))
        .add_yaxis("銀牌", [row['Nums'] for _, row in CN_medals[(CN_medals.Season=='Summer') & (CN_medals.Medal=='Silver')].iterrows()],
                  category_gap='20%',
                  itemstyle_opts=opts.ItemStyleOpts(
                                border_color='rgb(220,220,220)',
                                color=JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, 
                                             [{
                                                 offset: 0,
                                                 color: '#C0C0C0'
                                             }, {
                                                 offset: 1,
                                                 color: '#FFFFF0'
                                             }])""")))
        .add_yaxis("銅牌", [row['Nums'] for _, row in CN_medals[(CN_medals.Season=='Summer') & (CN_medals.Medal=='Bronze')].iterrows()],
                  category_gap='20%',
                  itemstyle_opts=opts.ItemStyleOpts(
                                border_color='rgb(220,220,220)',
                                color=JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, 
                                             [{
                                                 offset: 0,
                                                 color: '#DAA520'
                                             }, {
                                                 offset: 1,
                                                 color: '#FFFFF0'
                                             }])""")))
        .set_series_opts(label_opts=opts.LabelOpts(is_show=True, 
                                                position='top',
                                                font_style='italic'))
        .set_global_opts(
            title_opts=opts.TitleOpts(title="中國曆年奧運會獲得獎牌數數-夏奧會", pos_left='center'),
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
            legend_opts=opts.LegendOpts(is_show=False),
            yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63")),
            graphic_opts=[
            opts.GraphicImage(
                graphic_item=opts.GraphicItem(
                    id_="logo", right=0, top=0, z=-10, bounding="raw", origin=[75, 75]
                ),
                graphic_imagestyle_opts=opts.GraphicImageStyleOpts(
                    image="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586619952245&di=981a36305048f93eec791980acc99cf7&imgtype=0&src=http%3A%2F%2Fimg5.mtime.cn%2Fmg%2F2017%2F01%2F06%2F172210.42721559.jpg",
                    width=1000,
                    height=600,
                    opacity=0.6,),
            )
        ],)
        )

        
w_bar = (
        Bar(init_opts=opts.InitOpts(theme='dark', width='1000px', height='300px'))
        .add_xaxis(sorted(list(set([row['Year'] for _, row in CN_medals[CN_medals.Season=='Winter'].iterrows()])), reverse=True))
        .add_yaxis("金牌", [row['Nums'] for _, row in CN_medals[(CN_medals.Season=='Winter') & (CN_medals.Medal=='Gold')].iterrows()],
                  category_gap='20%',
                  itemstyle_opts=opts.ItemStyleOpts(
                                border_color='rgb(220,220,220)',
                                color=JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, 
                                             [{
                                                 offset: 0,
                                                 color: '#FFD700'
                                             }, {
                                                 offset: 1,
                                                 color: '#FFFFF0'
                                             }])""")))
        .add_yaxis("銀牌", [row['Nums'] for _, row in CN_medals[(CN_medals.Season=='Winter') & (CN_medals.Medal=='Silver')].iterrows()],
                  category_gap='20%',
                  itemstyle_opts=opts.ItemStyleOpts(
                                border_color='rgb(220,220,220)',
                                color=JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, 
                                             [{
                                                 offset: 0,
                                                 color: '#C0C0C0'
                                             }, {
                                                 offset: 1,
                                                 color: '#FFFFF0'
                                             }])""")))
        .add_yaxis("銅牌", [row['Nums'] for _, row in CN_medals[(CN_medals.Season=='Winter') & (CN_medals.Medal=='Bronze')].iterrows()],
                  category_gap='20%',
                  itemstyle_opts=opts.ItemStyleOpts(
                                border_color='rgb(220,220,220)',
                                color=JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, 
                                             [{
                                                 offset: 0,
                                                 color: '#DAA520'
                                             }, {
                                                 offset: 1,
                                                 color: '#FFFFF0'
                                             }])""")))
        .set_series_opts(label_opts=opts.LabelOpts(is_show=True, 
                                                position='top',
                                                font_style='italic'))
        .set_global_opts(
            title_opts=opts.TitleOpts(title="中國曆年奧運會獲得獎牌數-冬奧會", pos_left='center'),
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
            legend_opts=opts.LegendOpts(is_show=False),
            yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63")),
            graphic_opts=[
            opts.GraphicImage(
                graphic_item=opts.GraphicItem(
                    id_="logo", right=0, top=-300, z=-10, bounding="raw", origin=[75, 75]
                ),
                graphic_imagestyle_opts=opts.GraphicImageStyleOpts(
                    image="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586619952245&di=981a36305048f93eec791980acc99cf7&imgtype=0&src=http%3A%2F%2Fimg5.mtime.cn%2Fmg%2F2017%2F01%2F06%2F172210.42721559.jpg",
                    width=1000,
                    height=600,
                    opacity=0.6,),
            )
        ],)
        )


page = (
    Page()
    .add(s_bar,)
    .add(w_bar,)
)
page.render_notebook()

優勢專案

跳水,體操,射擊,舉重,乒乓球,羽毛球

background_color_js = (
    "new echarts.graphic.LinearGradient(1, 0, 0, 1, "
    "[{offset: 0.5, color: '#FFC0CB'}, {offset: 1, color: '#F0FFFF'}, {offset: 0, color: '#EE82EE'}], false)"
)


CN_events = CN_data[CN_data.Medal=='Gold'].groupby(['Year', 'Sport'])['Event'].nunique().reset_index()
CN_events = CN_events.groupby(['Sport'])['Event'].sum().reset_index()
CN_events.columns = ['Sport', 'Nums']                                      

data_pair = [(row['Sport'], row['Nums']) for _, row in CN_events.iterrows()]

wc = (WordCloud(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js), width='1000px', height='600px'))
     .add("", data_pair,word_size_range=[30, 80])
     .set_global_opts(title_opts=opts.TitleOpts(title="中國獲得過金牌運動專案",pos_left="center",
                                         title_textstyle_opts=opts.TextStyleOpts(color="white", font_size=20)))
)

wc.render_notebook()

??美國奧運會表現

USA_data = data[data.region=='USA']
USA_data.head()

歷屆奧運會參加人數

background_color_js = (
    "new echarts.graphic.LinearGradient(1, 0, 0, 1, "
    "[{offset: 0, color: '#008B8B'}, {offset: 1, color: '#FF6347'}], false)"
)



athlete = USA_data.groupby(['Year', 'Season'])['Name'].nunique().reset_index()
athlete.columns = ['Year', 'Season', 'Nums']                                      
athlete = athlete.sort_values(by="Year" , ascending=False) 


        
s_bar = (
        Bar(init_opts=opts.InitOpts(theme='dark', width='1000px', height='300px'))
        .add_xaxis([row['Year'] for _, row in athlete[athlete.Season=='Summer'].iterrows()])
        .add_yaxis("參賽人數", [row['Nums'] for _, row in athlete[athlete.Season=='Summer'].iterrows()],
                  category_gap='40%',
                  itemstyle_opts=opts.ItemStyleOpts(
                                border_color='rgb(220,220,220)',
                                color=JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, 
                                             [{
                                                 offset: 1,
                                                 color: '#00BFFF'
                                             }, {
                                                 offset: 0,
                                                 color: '#32CD32'
                                             }])""")))
        .set_series_opts(label_opts=opts.LabelOpts(is_show=True, 
                                                position='top',
                                                font_style='italic'))
        .set_global_opts(
            title_opts=opts.TitleOpts(title="美國曆年奧運會參賽人數-夏奧會", pos_left='center'),
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
            legend_opts=opts.LegendOpts(is_show=False),
            yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63")),
            graphic_opts=[
            opts.GraphicImage(
                graphic_item=opts.GraphicItem(
                    id_="logo", right=0, top=0, z=-10, bounding="raw", origin=[75, 75]
                ),
                graphic_imagestyle_opts=opts.GraphicImageStyleOpts(
                    image="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586619952245&di=981a36305048f93eec791980acc99cf7&imgtype=0&src=http%3A%2F%2Fimg5.mtime.cn%2Fmg%2F2017%2F01%2F06%2F172210.42721559.jpg",
                    width=1000,
                    height=600,
                    opacity=0.6,),
            )
        ],)
        )

        
w_bar = (
        Bar(init_opts=opts.InitOpts(theme='dark',width='1000px', height='300px'))
        .add_xaxis([row['Year'] for _, row in athlete[athlete.Season=='Winter'].iterrows()])
        .add_yaxis("參賽人數", [row['Nums'] for _, row in athlete[athlete.Season=='Winter'].iterrows()],
                  category_gap='50%',
                  itemstyle_opts=opts.ItemStyleOpts(
                                border_color='rgb(220,220,220)',
                                color=JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, 
                                             [{
                                                 offset: 1,
                                                 color: '#00BFFF'
                                             }, {
                                                 offset: 0.8,
                                                 color: '#FFC0CB'
                                             }, {
                                                 offset: 0,
                                                 color: '#40E0D0'
                                             }])""")))
        .set_series_opts(label_opts=opts.LabelOpts(is_show=True, 
                                                position='top',
                                                font_style='italic'))
        .set_global_opts(
            title_opts=opts.TitleOpts(title="美國曆年奧運會參賽人數-冬奧會", pos_left='center'),
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
            legend_opts=opts.LegendOpts(is_show=False),
            yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63")),
            graphic_opts=[
            opts.GraphicImage(
                graphic_item=opts.GraphicItem(
                    id_="logo", right=0, top=-300, z=-10, bounding="raw", origin=[75, 75]
                ),
                graphic_imagestyle_opts=opts.GraphicImageStyleOpts(
                    image="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586619952245&di=981a36305048f93eec791980acc99cf7&imgtype=0&src=http%3A%2F%2Fimg5.mtime.cn%2Fmg%2F2017%2F01%2F06%2F172210.42721559.jpg",
                    width=1000,
                    height=600,
                    opacity=0.6,),
            )
        ],)
        )


page = (
    Page()
    .add(s_bar,)
    .add(w_bar,)
)
page.render_notebook()

歷屆奧運會獲得獎牌數

background_color_js = (
    "new echarts.graphic.LinearGradient(1, 0, 0, 1, "
    "[{offset: 0, color: '#008B8B'}, {offset: 1, color: '#FF6347'}], false)"
)



medals = USA_data.groupby(['Year', 'Season', 'Medal'])['Event'].nunique().reset_index()
medals.columns = ['Year', 'Season', 'Medal', 'Nums']                                      
medals = medals.sort_values(by="Year" , ascending=False) 


        
s_bar = (
        Bar(init_opts=opts.InitOpts(theme='dark', width='1000px', height='300px'))
        .add_xaxis(sorted(list(set([row['Year'] for _, row in medals[medals.Season=='Summer'].iterrows()])), reverse=True))
        .add_yaxis("金牌", [row['Nums'] for _, row in medals[(medals.Season=='Summer') & (medals.Medal=='Gold')].iterrows()],
                  category_gap='20%',
                  itemstyle_opts=opts.ItemStyleOpts(
                                border_color='rgb(220,220,220)',
                                color=JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, 
                                             [{
                                                 offset: 0,
                                                 color: '#FFD700'
                                             }, {
                                                 offset: 1,
                                                 color: '#FFFFF0'
                                             }])""")))
        .add_yaxis("銀牌", [row['Nums'] for _, row in medals[(medals.Season=='Summer') & (medals.Medal=='Silver')].iterrows()],
                  category_gap='20%',
                  itemstyle_opts=opts.ItemStyleOpts(
                                border_color='rgb(220,220,220)',
                                color=JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, 
                                             [{
                                                 offset: 0,
                                                 color: '#C0C0C0'
                                             }, {
                                                 offset: 1,
                                                 color: '#FFFFF0'
                                             }])""")))
        .add_yaxis("銅牌", [row['Nums'] for _, row in medals[(medals.Season=='Summer') & (medals.Medal=='Bronze')].iterrows()],
                  category_gap='20%',
                  itemstyle_opts=opts.ItemStyleOpts(
                                border_color='rgb(220,220,220)',
                                color=JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, 
                                             [{
                                                 offset: 0,
                                                 color: '#DAA520'
                                             }, {
                                                 offset: 1,
                                                 color: '#FFFFF0'
                                             }])""")))
        .set_series_opts(label_opts=opts.LabelOpts(is_show=True, 
                                                position='top',
                                                font_style='italic'))
        .set_global_opts(
            title_opts=opts.TitleOpts(title="美國曆年奧運會獲得獎牌數數-夏奧會", pos_left='center'),
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
            legend_opts=opts.LegendOpts(is_show=False),
            yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63")),
            graphic_opts=[
            opts.GraphicImage(
                graphic_item=opts.GraphicItem(
                    id_="logo", right=0, top=0, z=-10, bounding="raw", origin=[75, 75]
                ),
                graphic_imagestyle_opts=opts.GraphicImageStyleOpts(
                    image="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586619952245&di=981a36305048f93eec791980acc99cf7&imgtype=0&src=http%3A%2F%2Fimg5.mtime.cn%2Fmg%2F2017%2F01%2F06%2F172210.42721559.jpg",
                    width=1000,
                    height=600,
                    opacity=0.6,),
            )
        ],)
        )

        
w_bar = (
        Bar(init_opts=opts.InitOpts(theme='dark', width='1000px', height='300px'))
        .add_xaxis(sorted(list(set([row['Year'] for _, row in medals[medals.Season=='Winter'].iterrows()])), reverse=True))
        .add_yaxis("金牌", [row['Nums'] for _, row in medals[(medals.Season=='Winter') & (medals.Medal=='Gold')].iterrows()],
                  category_gap='20%',
                  itemstyle_opts=opts.ItemStyleOpts(
                                border_color='rgb(220,220,220)',
                                color=JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, 
                                             [{
                                                 offset: 0,
                                                 color: '#FFD700'
                                             }, {
                                                 offset: 1,
                                                 color: '#FFFFF0'
                                             }])""")))
        .add_yaxis("銀牌", [row['Nums'] for _, row in medals[(medals.Season=='Winter') & (medals.Medal=='Silver')].iterrows()],
                  category_gap='20%',
                  itemstyle_opts=opts.ItemStyleOpts(
                                border_color='rgb(220,220,220)',
                                color=JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, 
                                             [{
                                                 offset: 0,
                                                 color: '#C0C0C0'
                                             }, {
                                                 offset: 1,
                                                 color: '#FFFFF0'
                                             }])""")))
        .add_yaxis("銅牌", [row['Nums'] for _, row in medals[(medals.Season=='Winter') & (medals.Medal=='Bronze')].iterrows()],
                  category_gap='20%',
                  itemstyle_opts=opts.ItemStyleOpts(
                                border_color='rgb(220,220,220)',
                                color=JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, 
                                             [{
                                                 offset: 0,
                                                 color: '#DAA520'
                                             }, {
                                                 offset: 1,
                                                 color: '#FFFFF0'
                                             }])""")))
        .set_series_opts(label_opts=opts.LabelOpts(is_show=True, 
                                                position='top',
                                                font_style='italic'))
        .set_global_opts(
            title_opts=opts.TitleOpts(title="美國曆年奧運會獲得獎牌數-冬奧會", pos_left='center'),
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
            legend_opts=opts.LegendOpts(is_show=False),
            yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63")),
            graphic_opts=[
            opts.GraphicImage(
                graphic_item=opts.GraphicItem(
                    id_="logo", right=0, top=-300, z=-10, bounding="raw", origin=[75, 75]
                ),
                graphic_imagestyle_opts=opts.GraphicImageStyleOpts(
                    image="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586619952245&di=981a36305048f93eec791980acc99cf7&imgtype=0&src=http%3A%2F%2Fimg5.mtime.cn%2Fmg%2F2017%2F01%2F06%2F172210.42721559.jpg",
                    width=1000,
                    height=600,
                    opacity=0.6,),
            )
        ],)
        )


page = (
    Page()
    .add(s_bar,)
    .add(w_bar,)
)
page.render_notebook()

優勢專案

田徑,游泳

background_color_js = (
    "new echarts.graphic.LinearGradient(1, 0, 0, 1, "
    "[{offset: 0.5, color: '#FFC0CB'}, {offset: 1, color: '#F0FFFF'}, {offset: 0, color: '#EE82EE'}], false)"
)


events = USA_data[USA_data.Medal=='Gold'].groupby(['Year', 'Sport'])['Event'].nunique().reset_index()
events = events.groupby(['Sport'])['Event'].sum().reset_index()
events.columns = ['Sport', 'Nums']                                      

data_pair = [(row['Sport'], row['Nums']) for _, row in events.iterrows()]

wc = (WordCloud(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js), width='1000px', height='600px'))
     .add("", data_pair,word_size_range=[30, 80])
     .set_global_opts(title_opts=opts.TitleOpts(title="美國獲得過金牌運動專案",pos_left="center",
                                         title_textstyle_opts=opts.TextStyleOpts(color="white", font_size=20)))
)

wc.render_notebook()

被單個國家統治的奧運會專案

很多運動長期以來一直是被某個國家統治,譬如我們熟知的中國??的乒乓球,美國??的籃球;

此次篩選了近5屆奧運會(2000年悉尼奧運會之後)上累計產生10枚金牌以上且存在單個國家「奪金率」超過50%的專案

  • 俄羅斯??包攬了2000年以後花樣游泳 & 藝術體操兩個專案上所有的20枚金牌;

  • 中國??在乒乓球專案上獲得了2000年之後10枚金牌中的9枚,丟失金牌的一次是在04年雅典奧運會男單專案上;

  • 美國??在籃球專案上同樣獲得了過去10枚金牌中的9枚,丟失金牌的一次同樣在04年,男籃半決賽中輸給了阿根廷,最終獲得銅牌;

  • 跳水專案上,中國??獲得了過去40枚金牌中的31枚,夢之隊名不虛傳;

  • 射箭專案上,韓國??獲得了過去20枚金牌中的15枚;

  • 羽毛球專案上,中國??獲得了過去25枚金牌中的17枚

  • 沙灘排球專案上,美國??獲得了過去10枚金牌中的5枚;

f1 = lambda x:max(x['Event']) / sum(x['Event'])
f2 = lambda x: x.sort_values('Event', ascending=False).head(1)

t_data = data[(data.Medal=='Gold') & (data.Year>=2000) &(data.Season=='Summer')].groupby(['Year', 'Sport', 'region'])['Event'].nunique().reset_index()
t_data = t_data.groupby(['Sport', 'region'])['Event'].sum().reset_index()
t1 = t_data.groupby(['Sport']).apply(f2).reset_index(drop=True)
t2 = t_data.groupby(['Sport'])['Event'].sum().reset_index()
t_data = pd.merge(t1, t2, on='Sport', how='inner')
t_data['gold_rate'] = t_data.Event_x/ t_data.Event_y
t_data = t_data.sort_values('gold_rate', ascending=False).reset_index(drop=True)

t_data = t_data[(t_data.gold_rate>=0.5) & (t_data.Event_y>=10)]



background_color_js = (
    "new echarts.graphic.LinearGradient(1, 0, 0, 1, "
    "[{offset: 0, color: '#008B8B'}, {offset: 1, color: '#FF6347'}], false)"
)

fn = """
    function(params) {
        if(params.name == '其他國家')
            return '\\n\\n\\n' + params.name + ' : ' + params.value ;
        return params.seriesName+ '\\n' + params.name + ' : ' + params.value;
    }
    """


def new_label_opts():
    return opts.LabelOpts(formatter=JsCode(fn), position="center")


pie = Pie(init_opts=opts.InitOpts(theme='dark', width='1000px', height='1000px'))
idx = 0

for _, row in t_data.iterrows():
    
    if idx % 2 == 0:
        x = 30
        y = int(idx/2) * 22 + 18
    else:
        x = 70
        y = int(idx/2) * 22 + 18
    idx += 1
    pos_x = str(x)+'%'
    pos_y = str(y)+'%'
    pie.add(
            row['Sport'],
            [[row['region'], row['Event_x']], ['其他國家', row['Event_y']-row['Event_x']]],
            center=[pos_x, pos_y],
            radius=[70, 100],
            label_opts=new_label_opts(),)
    
pie.set_global_opts(
        title_opts=opts.TitleOpts(title="被單個國家統治的專案",
                                  subtitle='統計週期:2000年悉尼奧運會起',
                                  pos_left="center",
                                  title_textstyle_opts=opts.TextStyleOpts(color="white", font_size=20)),
        legend_opts=opts.LegendOpts(is_show=False),
    )


pie.render_notebook()


歡迎點贊支援❤️???

相關文章