Py.Cafe

huong-li-nguyen/

figure-friday-week-29

English Women's Football - Match Outcomes Analysis

DocsPricing
  • app.py
  • logo.svg
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# check out https://github.com/mckinsey/vizro for more info about Vizro
# and checkout https://vizro.readthedocs.io/en/stable/ for documentation

from typing import Literal

import pandas as pd
import plotly.express as px
import vizro.models as vm
import vizro.plotly.express as px
from vizro import Vizro
from vizro.models.types import capture

# Load the data
df = pd.read_csv("https://raw.githubusercontent.com/plotly/Figure-Friday/main/2024/week-29/ewf_standings.csv")


@capture("graph")
def stacked_bar(data_frame, baseline_category: Literal["wins", "draws", "losses"] = "wins"):
    """Create a stacked 100% bar chart for the selected team."""
    # Change default order based on chosen baseline_category
    default_order = ["wins", "draws", "losses"]
    x_order = [baseline_category] + [item for item in default_order if item != baseline_category]

    # Create stacked 100% bar chart
    fig = px.histogram(
        data_frame,
        y="season",
        x=x_order,
        barnorm="fraction",
        text_auto=".0%",
        color_discrete_map={"wins": "#625AD8", "draws": "#d4d4d4", "losses": "#97dffc"},
    )

    fig.update_layout(
        xaxis=dict(
            title="Share of Total Matches",
            tickmode="array",
            tickvals=[0, 0.2, 0.4, 0.6, 0.8, 1],
            ticktext=["0%", "20%", "40%", "60%", "80%", "100%"],
        ),
        yaxis_title="Season",
        legend_title=None,
        hovermode=False,
        # Make title dynamic (reactive to controls)
        title=f"Seasonal Win, Draw, Loss Distribution for <b>{data_frame['team_name'].iloc[0]}<b>",
    )
    return fig


page = vm.Page(
    title="Week 29 - English Women's Football ⚽",
    components=[
        vm.Graph(figure=stacked_bar(data_frame=df), id="stacked-bar"),
    ],
    controls=[
        vm.Filter(
            column="team_name", selector=vm.Dropdown(multi=False, title="Select a team:", value="Manchester City Women")
        ),
        vm.Parameter(
            targets=["stacked-bar.baseline_category"],
            selector=vm.RadioItems(
                options=["wins", "draws", "losses"], value="wins", title="Select baseline category:"
            ),
        ),
    ],
)

dashboard = vm.Dashboard(pages=[page], theme="vizro_light", title="Figure Friday")
Vizro(assets_folder=".").build(dashboard).run()