[Python visualization] visual analysis of the Olympic Games with Pyecharts ๏ฝž

All code & datasets of the project can be accessedMy KLab -- [Pyecharts] Olympic Games data set visualization analysis ~ Get it, click Fork ~

  • Affected by the epidemic, the 2020 Tokyo Olympic Games will be postponed to 2021;

  • Although it is postponed, the name "Tokyo 2020 Olympic Games" will still be used in this Olympic Games;

  • It will also be the first time in the history of the Olympic Games to be postponed (1916, 1940 and 1944 were closed due to World War I and World War II);

Since the Olympic Games have been postponed, let's review the history of the whole Olympic Games ๐ŸŽ‰๐ŸŽ‰ ~

This project will present the history of the Olympic Games from the following perspectives:

  1. A kind of Cumulative number of medals by country;

  2. A kind of Number of gold medals produced in various sports

  3. A kind of Athlete level

    • Trends in number of participants

    • Trends in female participation

    • The athlete who won the most gold medals

    • Proportion of medals / gold medals won

    • Average physical fitness data of athletes in each event

  4. Major country performance

    • ๐Ÿ‡จ๐Ÿ‡ณ China Performance

    • ๐Ÿ‡บ๐Ÿ‡ธ Us performance

  5. A kind of Olympic events dominated by individual countries

Import library & Data

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')

# Related representative country
data = pd.merge(athlete_data, noc_region, on='NOC', how='left')
data.head()

Cumulative medals

Statistics of Summer Olympic Games and Winter Olympic Games

  • The Summer Olympics began in 1896 in Athens;

  • A kind of The Winter Olympics began in 1924 in Munich;

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) 

Accumulated medals of Summer Olympic Games in different countries

  • As of the 2016 Summer Olympic Games, the United States and Russia respectively won 2544 and 1577 medals, ranking first and second;

  • Due to its late participation in the Olympic Games, China has won 545 medals by 2016, ranking seventh;

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("Bronze Medal๐Ÿฅ‰", [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("Silver medal๐Ÿฅˆ", [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("gold medal๐Ÿ…๏ธ", [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="Cumulative medals by country (Summer Olympics)"),
                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()

Accumulated medals in Winter Olympic Games of various countries

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("Bronze Medal๐Ÿฅ‰", [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("Silver medal๐Ÿฅˆ", [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("gold medal๐Ÿ…๏ธ", [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="Cumulative medals by country (Winter Olympics)"),
                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='End{}'.format(year),
                                                           font="bold 26px Microsoft YaHei",
                                                           graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
                                                               fill="#fff"
                                                           ),
                                                       ),
                                                   ),
                                               ],
                                            )
                                    ],)
            .reversal_axis())
    tl.add(bar, year)

tl.render_notebook()

Number of gold medals produced in various sports

Based on the statistics of 2016 Summer Olympic Games and 2014 Winter Olympic Games;

  • A kind of Track and field swimming is a major event, with 47 and 34 gold medals in 2016 Summer Olympic Games;
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('gold medal๐Ÿ…๏ธ', [(row['Sport'], row['Nums']) for _, row in event_medal.iterrows()],
            radius=["30%", "75%"],
            rosetype="radius")
       .set_global_opts(title_opts=opts.TitleOpts(title="2016 Proportion of gold medals produced in various sports in summer Olympic Games", 
                                                  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 Summer Olympics')

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('gold medal๐Ÿ…๏ธ', [(row['Sport'], row['Nums']) for _, row in event_medal.iterrows()],
            radius=["30%", "75%"],
            rosetype="radius")
       .set_global_opts(title_opts=opts.TitleOpts(title="2014 Proportion of gold medals produced in various sports in the Winter Olympic Games", 
                                                  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 Winter Olympics')
tab.render_notebook()

Athlete level

Trend of number of participants over the years

  • In terms of number of participants, the number of participants in each summer Olympic Games is 4-5 times of that in Winter Olympic Games;

  • The overall number of participants is on the rise, but there have also been fluctuations due to historical reasons, such as the 1980 Moscow Olympic Games encountered resistance from 65 countries;

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("Summer Olympics", 
        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("Winter Olympics", 
        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="the First World War", x=(1914, 1918)),
                opts.MarkAreaItem(name="the Second World War", x=(1939, 1945)),
            ]
        )
    )
    .set_global_opts(title_opts=opts.TitleOpts(title="Number of participants in previous Olympic Games",
                                                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+'year';}""")),
                                                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()

The proportion trend of female athletes over the years

At the beginning, the Olympic Games were basically "men's sports". The number of female athletes was only one digit, and in recent Olympic Games, the number of male and female athletes was almost equal;

# Number of male athletes over the years
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) 

# Number of female athletes over the years
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("Summer Olympics", 
        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("Winter Olympics", 
        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]+ 'year: ' + Number(params.data[1])*100 +'%';}""")),)
    .set_global_opts(title_opts=opts.TitleOpts(title="The trend of women's proportion in previous Olympic Games",
                                                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+'year';}""")),
                                                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()

The athlete who won the most gold medals

  • In the first place is Phelps, the famous American swimmer, who has won 23 gold medals as of the 2016 Olympic Games;

  • Bolt has won 8 Olympic gold medals;

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="The athletes who won the most gold medals", 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()

Gold medal / Medal percentage

It's hard for Phelps to win a gold medal, but is it really difficult to win a gold medal?

  • In the history of the whole Olympic Games (including summer and Winter Olympic Games), the number of participants was 134732, and only 10413 athletes had won the gold medal, accounting for 7.7%;

  • There are 28202 athletes who have won medals (including gold, silver and copper), accounting for 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("Win a medal", [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="Proportion of medals won", 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 medal",
        [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="Percentage of gold medals won", 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()

Average physical fitness data of athletes

Statistics according to different sports

  • Basketball is the sport with the highest average height of athletes. The average height of women is 182cm and that of men is 194cm;

  • Among the men's events, tug of war is the most important one, with an average weight of 96 kg (the tug of war has been cancelled since the seventh Olympic Games);

  • The biggest event of the average age of athletes is Art competition, with an average age of 46 years. In addition, it is equestrian and shooting. The average age of men is 34.4 and 34.2, respectively. The average age of women is 34.22 and 29.12s;

tool_js = """function (param) {return param.data[2] +'<br/>' 
            +'Average weight: '+Number(param.data[0]).toFixed(2)+' kg<br/>'
            +'Average height: '+Number(param.data[1]).toFixed(2)+' cm<br/>'
            +'average age: '+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("Male", [[row['Height'], row['Sport'], row['Age']] for _, row in temp_data.iterrows()],
                      # Gradient effect implementation part
                      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="Average age of weight gain of athletes in each event",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='weight/kg',
                   # Set axis to numerical type
                   type_="value", 
                   is_scale=True,
                   # Show split lines
                   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='height/cm',
                   # Set axis to numerical type
                   type_="value",
                   # The default is False, which means the starting point is 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("Female sex", [[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() 

๐Ÿ‡จ๐Ÿ‡ณ China Olympic performance

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

Number of participants in previous Olympic Games

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("Number of participants", [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="Number of participants in Chinese Olympic Games over the years-Summer Olympic Games", 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("Number of participants", [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="Number of participants in Chinese Olympic Games over the years-Winter Olympics", 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()

Medals of previous Olympic Games

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("gold medal", [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("Silver medal", [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("Bronze Medal", [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="The number of medals won by Chinese Olympic Games over the years-Summer Olympic Games", 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("gold medal", [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("Silver medal", [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("Bronze Medal", [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="The number of medals won by Chinese Olympic Games over the years-Winter Olympics", 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()

Advantage project

Diving, gymnastics, shooting, weightlifting, table tennis, badminton

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="China has won gold medals in sports",pos_left="center",
                                         title_textstyle_opts=opts.TextStyleOpts(color="white", font_size=20)))
)

wc.render_notebook()

๐Ÿ‡บ๐Ÿ‡ธ US Olympic performance

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

Number of participants in previous Olympic Games

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("Number of participants", [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="Number of participants in the United States Olympic Games over the years-Summer Olympic Games", 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("Number of participants", [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="Number of participants in the United States Olympic Games over the years-Winter Olympics", 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()

Number of medals won in previous Olympic Games

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("gold medal", [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("Silver medal", [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("Bronze Medal", [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="The number of medals won in the United States Olympic Games over the years-Summer Olympic Games", 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("gold medal", [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("Silver medal", [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("Bronze Medal", [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="Number of medals won in the United States Olympic Games over the years-Winter Olympics", 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()

Advantage project

Track and field, swimming

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="The United States has won gold medals in sports",pos_left="center",
                                         title_textstyle_opts=opts.TextStyleOpts(color="white", font_size=20)))
)

wc.render_notebook()

Olympic events dominated by individual countries

Many movements have long been ruled by a certain country, such as China ๐Ÿ‡จ๐Ÿ‡ณ Table tennis, USA ๐Ÿ‡บ๐Ÿ‡ธ Basketball;

This time, more than 10 gold medals have been accumulated in the past five Olympic Games (after the 2000 Sydney Olympic Games) and there are more than 50% gold medal winning events in a single country;

  • Russia ๐Ÿ‡ท๐Ÿ‡บ After 2000, he won all 20 gold medals in synchronized swimming and rhythmic gymnastics;

  • China ๐Ÿ‡จ๐Ÿ‡ณ In table tennis, he won 9 of the 10 gold medals after 2000, and lost the gold medal in the men's singles of 2004 Athens Olympic Games;

  • U.S.A ๐Ÿ‡บ๐Ÿ‡ธ In basketball, he also won 9 of the last 10 gold medals, and lost the gold medal in 2004. He lost to Argentina in the semi-finals of the men's basketball team and finally won the bronze medal;

  • Diving, China ๐Ÿ‡จ๐Ÿ‡ณ It has won 31 of the past 40 gold medals, and the dream team is well-known;

  • Archery, Korea ๐Ÿ‡ฐ๐Ÿ‡ท Won 15 of the past 20 gold medals;

  • Badminton, China ๐Ÿ‡จ๐Ÿ‡ณ It has won 17 of the past 25 gold medals;

  • Beach volleyball, USA ๐Ÿ‡บ๐Ÿ‡ธ Won 5 of the last 10 gold medals;

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 == 'Other countries')
            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']], ['Other countries', 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="Projects governed by individual countries",
                                  subtitle='Statistical cycle: since 2000 Sydney Olympic Games',
                                  pos_left="center",
                                  title_textstyle_opts=opts.TextStyleOpts(color="white", font_size=20)),
        legend_opts=opts.LegendOpts(is_show=False),
    )


pie.render_notebook()

Welcome to like support Mei A kind of ๐Ÿ’š๐Ÿ’›๐Ÿ’œ

Tags: Lambda

Posted on Wed, 29 Apr 2020 18:01:04 -0700 by mlefebvre