“This is the 21st day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

In this paper, we use pandas and Pyecharts to create a static large screen based on the data of the 2020 Olympic Games

Design a large screen layout

The map

Map drawing is relatively simple. English names and total MEDALS in the data can be directly used as map data to achieve map drawing

Compared with the thermal value display, the legend adopts the way of three equal points, so the number of awards is more intuitive

Most of the subsequent charts use the theme of light

import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Map

df = pd.read_excel("Medal list data set.xlsx")
data = [[i,j] for i,j in zip(df['English name'], df['Total MEDALS'])]

charts =  Map(init_opts=opts.InitOpts(theme='light'))
charts.add("Total number of MEDALS", data, "world",is_map_symbol_show=False)
charts.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
charts.set_global_opts(title_opts=opts.TitleOpts(title=Gold Medal Distribution for 2020 Tokyo Olympic Games),
                       visualmap_opts=opts.VisualMapOpts(max_=120,is_piecewise=True,split_number=3))
charts.render_notebook()
Copy the code

Horizontal stack diagram

Jpyecharts calls a reversal_axis() function when drawing a horizontal picture. Be sure that the data also needs to be reversed, so use [::-1] to reverse the data.

Color collocation author weak chicken one, corresponding gold medal, silver medal, bronze medal to find three can see the color match.

from pyecharts.charts import Bar
from pyecharts import options as opts

df = pd.read_excel("Medal list data set.xlsx")
df = df.sort_values(by="Total medal count",axis=0,ascending=False)
nation = df['name'].values[:10] [: : -1].tolist()
gold = df['gold'].values[:10] [: : -1].tolist()
silver = df['silver'].values[:10] [: : -1].tolist()
copper = df['bronze'].values[:10] [: : -1].tolist()

bar = Bar(init_opts=opts.InitOpts(width='1000px',height='600px'))
bar.add_xaxis(nation)
Series with the same value will be stacked on top of each other
bar.add_yaxis('gold', gold, stack='stack1',category_gap="50%",color="#e5b751")
bar.add_yaxis('silver', silver, stack='stack1',category_gap="50%",color="#f1f0ed")
bar.add_yaxis('bronze', copper, stack='stack1',category_gap="50%",color="#fed71a")
bar.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
bar.set_global_opts(title_opts=opts.TitleOpts(title="TOP10 in Tokyo Olympic medal tally"))
bar.reversal_axis()
bar.render_notebook()
Copy the code

The pie chart

The pie chart shows the awards of each event in China.

After locating the data to China, it is good to use groupby to take the names of statistical items and arrange them in descending order.

Use the enumerated sequence I to set the position of XY for each pie chart

import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Pie

df = pd.read_excel("Dataset of participating athletes. XLSX")
region_list = ["China"."Hong Kong, China"."Chinese Taipei"]
titles = []
pie = Pie(
    init_opts=opts.InitOpts(
        theme='light',
        width='1000px',
        height='800px')),for i,r in enumerate(region_list):
    
    sortdata = df[df["Country"]== r].groupby('Project name') ['Project name'].count().sort_values(0, ascending=False)
    names = sortdata.index.tolist()
    values = sortdata.values.tolist()
    dataItem = [list(z) for z in zip(names,  values)]
    
    pos_x = '{} %'.format(int(i / 3) * 33 + 18)
    pos_y = '{} %'.format(i % 3 * 28 + 33)
    
    titles.append(dict(text=r+' ',
                       left=pos_x,
                       top=pos_y,
                       textAlign='center',
                       textVerticalAlign='middle',
                       textStyle=dict(color='#603d30',fontSize=12))
                 )
    
    pie.add(
        r,
        dataItem,
        center=[pos_x, pos_y],
        radius=['8%'.'12%'],
        label_opts=opts.LabelOpts(is_show=True,formatter='{b}:{d}%')
    )
    


pie.set_global_opts(
    legend_opts=opts.LegendOpts(is_show=False),
    title_opts=titles)
pie.render_notebook()
Copy the code

The timeline

Learn from the production method of AwesomeTang

df = pd.read_excel("Dataset of participating athletes. XLSX")
y_data = []
counter = 0
position = ['left'.'right']
for idx, row in df[(df['English abbreviation'] = ='CHN') & (df['Gold Type'] = =1)].iterrows():
    msg = '{bbb|%s}\n{aaa|%s}\n{bbb|%s/%s}' % (row['Award Time'] [:10], row['sportsman'], row['Project name'], row['Subproject name'])
    # Single data item configuration
    l_item = opts.LineItem(
        name=10,
        value=counter,
        symbol='emptyCircle',
        symbol_size=10,
        label_opts=opts.LabelOpts(
            is_show=True, 
            font_size=16,
            position=position[counter%2], 
            formatter=msg,
            rich = {
                'aaa': {
                    'fontSize': 18.'color': 'red'.'fontWeight':'bold'.'align':position[(counter+1) %2],},'bbb': {
                    'fontSize': 15.'color': '# 000'.'align':position[(counter+1) %2]}}
            )
    )
    y_data.append(l_item)
    counter+=1
    
line = Line(
    init_opts=opts.InitOpts(
        theme='light',
        width='1000px',
        height='2000px',
        bg_color='white'
    )
)
line.add_xaxis(
    ['CHN']
)
line.add_yaxis(
    ' ',
    y_data,
    linestyle_opts={
        'normal': {
            'width': 4.Set the line width
            'color':'red'.'shadowColor': 'rgba(155, 18, 184, .3)'.# Shadow color
            'shadowBlur': 10.# Shadow size
            'shadowOffsetY': 10.# Y direction shadow offset
            'shadowOffsetX': 10.# X-axis shadow offset
        }
    },
    itemstyle_opts={
        'normal': {
            'color':'red'.'shadowColor': 'rgba(155, 18, 184, .3)'.# Shadow color
            'shadowBlur': 10.# Shadow size
            'shadowOffsetY': 10.# Y direction shadow offset
            'shadowOffsetX': 10.# X-axis shadow offset
        }
    },
    tooltip_opts=opts.TooltipOpts(is_show=False)
)

line.set_global_opts(
    xaxis_opts=opts.AxisOpts(is_show=False, type_='category'),
    yaxis_opts=opts.AxisOpts(is_show=False, type_='value', max_=len(y_data)),
    title_opts=opts.TitleOpts(
        title="Time for gold.", pos_left='center', pos_top='2%',
        title_textstyle_opts=opts.TextStyleOpts(color='red', font_size=20)
    ),
    graphic_opts=[
                opts.GraphicGroup(
                            graphic_item=opts.GraphicItem(id_='1',left="center", top="center", z=-1),
                            children=[# tokyo 
                                    opts.GraphicImage(graphic_item=opts.GraphicItem(id_="logo",
                                                                                    left='center',
                                                                                    z=-1),
                                                      graphic_imagestyle_opts=opts.GraphicImageStyleOpts(
                                        image="https://olympics.com/tokyo-2020/en/d3images/emblem/olympics/emblem-tokyo2020.svg",
                                        width=800,
                                        height=1000,
                                        opacity=0.1,)
                                    )
                                ]
                                )
                                ]
)
line.render_notebook()
Copy the code

Large screen layout of data

Modify the above four charts as functions to combine the charts using Page

The complete code

# https://www.233tw.com/python/59145
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import *


def map_world()-> Map:
    df = pd.read_excel("Medal list data set.xlsx")
    data = [[i,j] for i,j in zip(df['English name'], df['Total MEDALS'])]

    charts =  Map()
    charts.add("Total number of MEDALS", data, "world",is_map_symbol_show=False)
    charts.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    charts.set_global_opts(title_opts=opts.TitleOpts(title=Gold Medal Distribution for 2020 Tokyo Olympic Games),
                           visualmap_opts=opts.VisualMapOpts(max_=120,is_piecewise=True,split_number=3))
    return charts

def bar_medals()->Bar:
    df = pd.read_excel("Medal list data set.xlsx")
    df = df.sort_values(by="Total medal count",axis=0,ascending=False)
    nation = df['name'].values[:10] [: : -1].tolist()
    gold = df['gold'].values[:10] [: : -1].tolist()
    silver = df['silver'].values[:10] [: : -1].tolist()
    copper = df['bronze'].values[:10] [: : -1].tolist()

    bar = Bar(init_opts=opts.InitOpts(width='1000px',height='600px'))
    bar.add_xaxis(nation)
    Series with the same value will be stacked on top of each other
    bar.add_yaxis('gold', gold, stack='stack1',category_gap="50%",color="#e5b751")
    bar.add_yaxis('silver', silver, stack='stack1',category_gap="50%",color="#f1f0ed")
    bar.add_yaxis('bronze', copper, stack='stack1',category_gap="50%",color="#fed71a")
    bar.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    bar.set_global_opts(title_opts=opts.TitleOpts(title="TOP10 in Tokyo Olympic medal tally"))
    bar.reversal_axis()
    return bar

def pie_china()->Pie:
    df = pd.read_excel("Dataset of participating athletes. XLSX")
    region_list = ["China"."Hong Kong, China"."Chinese Taipei"]
    titles = []
    pie = Pie(
        init_opts=opts.InitOpts(
            theme='light',
            width='1000px',
            height='800px')),for i,r in enumerate(region_list):

        sortdata = df[df["Country"]== r].groupby('Project name') ['Project name'].count().sort_values(0, ascending=False)
        names = sortdata.index.tolist()
        values = sortdata.values.tolist()
        dataItem = [list(z) for z in zip(names,  values)]
        pos_x = '{} %'.format(int(i / 3) * 33 + 18)
        pos_y = '{} %'.format(i % 3 * 28 + 33)
        titles.append(dict(text=r+' ',
                           left=pos_x,
                           top=pos_y,
                           textAlign='center',
                           textVerticalAlign='middle',
                           textStyle=dict(color='#603d30',fontSize=12)))
        pie.add(r,dataItem,center=[pos_x, pos_y],radius=['8%'.'12%'],label_opts=opts.LabelOpts(is_show=True,formatter='{b}:{d}%'))
    pie.set_global_opts(legend_opts=opts.LegendOpts(is_show=False),title_opts=titles)
    return pie

def timeline()->Line:
    df = pd.read_excel("Dataset of participating athletes. XLSX")
    y_data = []
    counter = 0
    position = ['left'.'right']
    for idx, row in df[(df['English abbreviation'] = ='CHN') & (df['Gold Type'] = =1)].iterrows():
        msg = '{bbb|%s}\n{aaa|%s}\n{bbb|%s/%s}' % (row['Award Time'] [:10], row['sportsman'], row['Project name'], row['Subproject name'])
        # Single data item configuration
        l_item = opts.LineItem(
            name=10,
            value=counter,
            symbol='emptyCircle',
            symbol_size=10,
            label_opts=opts.LabelOpts(
                is_show=True, 
                font_size=16,
                position=position[counter%2], 
                formatter=msg,
                rich = {
                    'aaa': {
                        'fontSize': 18.'color': 'red'.'fontWeight':'bold'.'align':position[(counter+1) %2],},'bbb': {
                        'fontSize': 15.'color': '# 000'.'align':position[(counter+1) %2]}}
                )
        )
        y_data.append(l_item)
        counter+=1

    line = Line(
        init_opts=opts.InitOpts(
            theme='light',
            width='1000px',
            height='2000px',
            bg_color='white'
        )
    )
    line.add_xaxis(
        ['CHN']
    )
    line.add_yaxis(
        ' ',
        y_data,
        linestyle_opts={
            'normal': {
                'width': 4.Set the line width
                'color':'red'.'shadowColor': 'rgba(155, 18, 184, .3)'.# Shadow color
                'shadowBlur': 10.# Shadow size
                'shadowOffsetY': 10.# Y direction shadow offset
                'shadowOffsetX': 10.# X-axis shadow offset
            }
        },
        itemstyle_opts={
            'normal': {
                'color':'red'.'shadowColor': 'rgba(155, 18, 184, .3)'.# Shadow color
                'shadowBlur': 10.# Shadow size
                'shadowOffsetY': 10.# Y direction shadow offset
                'shadowOffsetX': 10.# X-axis shadow offset
            }
        },
        tooltip_opts=opts.TooltipOpts(is_show=False)
    )

    line.set_global_opts(
        xaxis_opts=opts.AxisOpts(is_show=False, type_='category'),
        yaxis_opts=opts.AxisOpts(is_show=False, type_='value', max_=len(y_data)),
        title_opts=opts.TitleOpts(
            title="Time for gold.", pos_left='center', pos_top='2%',
            title_textstyle_opts=opts.TextStyleOpts(color='red', font_size=20)
        ),
        graphic_opts=[
                    opts.GraphicGroup(
                                graphic_item=opts.GraphicItem(id_='1',left="center", top="center", z=-1),
                                children=[# tokyo 
                                        opts.GraphicImage(graphic_item=opts.GraphicItem(id_="logo",
                                                                                        left='center',
                                                                                        z=-1),
                                                          graphic_imagestyle_opts=opts.GraphicImageStyleOpts(
                                            image="https://olympics.com/tokyo-2020/en/d3images/emblem/olympics/emblem-tokyo2020.svg",
                                            width=800,
                                            height=1000,
                                            opacity=0.1,))])])return line

page = Page(layout=Page.DraggablePageLayout, page_title="Tokyo 2020 Data Visualization")

# Add charts to the page
page.add(
    timeline(),
    map_world(),
    bar_medals(),
    pie_china(),

)
page.load_javascript()
page.render("draghtmlpage.html")
Copy the code

Set the layout to Page.DraggablePageLayout to drag and drop images to customize the layout.

Open the saved HTML custom layout and click Save Config to save the JSON file for the layout parameters.

Finally, run the following line to call the saved layout file to regenerate the HTML

a = page.save_resize_html('draghtmlpage.html', cfg_file='chart_config.json', dest='Olympics. HTML')
Copy the code

Effect:

Preliminary realization of data visualization large screen display, there are still many deficiencies, talent, mistakes or imperfect place, please criticize and correct!!

References:

[1] [Tokyo 2020 Olympic Games] Data visualization ~

[2] The Games are pandas

[3] Python visualization of large screens (Tokyo Olympics)