gt-extras
  • Intro
  • API Reference
  1. Plotting
  2. gt_plt_winloss
  • API Reference
  • Plotting
    • gt_plt_bar
    • gt_plt_bar_pct
    • gt_plt_bar_stack
    • gt_plt_conf_int
    • gt_plt_dot
    • gt_plt_dumbbell
    • gt_plt_winloss
  • Colors
    • gt_color_box
    • gt_data_color_by_group
    • gt_highlight_cols
    • gt_highlight_rows
    • gt_hulk_col_numeric
  • Themes
    • gt_theme_538
    • gt_theme_dark
    • gt_theme_dot_matrix
    • gt_theme_espn
    • gt_theme_excel
    • gt_theme_guardian
    • gt_theme_nytimes
    • gt_theme_pff
  • Icons and Images
    • add_text_img
    • fa_icon_repeat
    • gt_fa_rank_change
    • gt_fa_rating
    • img_header
  • Utilities
    • fmt_pct_extra
    • gt_add_divider
    • gt_duplicate_column
    • gt_merge_stack
    • gt_two_column_layout
    • with_hyperlink
    • with_tooltip

On this page

  • Parameters
  • Returns
  • Examples
  1. Plotting
  2. gt_plt_winloss

gt_plt_winloss

gt_plt_winloss(
    gt,
    column,
    width=80,
    height=30,
    win_color='blue',
    loss_color='red',
    tie_color='grey',
    shape='pill',
    spacing=2,
)

Create win/loss charts in GT cells.

The gt_plt_winloss() function takes an existing GT object and adds win/loss sparkline charts to a specified column. Each cell displays a series of small vertical bars representing individual game outcomes, This visualization is useful for showing performance streaks and patterns over time. All win/loss charts are scaled to accommodate the longest sequence in the column, ensuring consistent bar spacing across all rows.

Wins must be represented as 1, ties as 0.5, and losses as 0. Invalid values (not 0, 0.5, or 1) are skipped.

Parameters

gt : GT

A GT object to modify.

column : SelectExpr

The column containing lists of win/loss/tie values. Each cell should contain a list where: 1 represents a win, 0 represents a loss, and 0.5 represents a tie. Values that are not listed above are skipped.

width : float = 80

The width of the win/loss chart in pixels.

height : float = 30

The height of the win/loss chart in pixels.

win_color : str = 'blue'

The color for bars representing wins.

loss_color : str = 'red'

The color for bars representing losses.

tie_color : str = 'grey'

The color for bars representing ties.

shape : Literal['pill', 'square'] = 'pill'

The shape style of the bars. Options are "pill" for taller bars or "square" for stockier, nearly square bars.

spacing : float = 2

The horizontal gap, in pixels, between each bar. Note that if the spacing is too large, it may obstruct the bars from view.

Returns

: GT

A GT object with win/loss charts added to the specified column.

Examples

First, let’s make a table with randomly generated data:

from great_tables import GT, md
import gt_extras as gte
import pandas as pd

df = pd.DataFrame(
    {
        "Team": ["Liverpool", "Chelsea", "Man City"],
        "10 Games": [
            [1, 1, 0, 1, 0.5, 1, 0, 1, 1, 0],
            [0, 0, 1, 0, 1, 1, 1, 0, 1, 1],
            [0.5, 1, 0.5, 0, 1, 0, 1, 0.5, 1, 0],
        ],
    }
)

gt = GT(df)

gt.pipe(
    gte.gt_plt_winloss,
    column="10 Games",
    win_color="green",
)
Team 10 Games
Liverpool
Chelsea
Man City

Let’s do a more involved example using NFL season data from 2016.

Show the setup Code
# Load the NFL data
df = pd.read_csv("../assets/games.csv")
season_2016 = df[(df["season"] == 2016) & (df["game_type"] == "REG")].copy()

def get_team_results(games_df):
    results = {}

    for _, game in games_df.iterrows():
        away_team = game["away_team"]
        home_team = game["home_team"]
        away_score = game["away_score"]
        home_score = game["home_score"]

        if away_team not in results:
            results[away_team] = []
        if home_team not in results:
            results[home_team] = []

        if away_score > home_score:
            results[away_team].append(1)
            results[home_team].append(0)
        elif home_score > away_score:
            results[home_team].append(1)
            results[away_team].append(0)
        else:
            results[away_team].append(0.5)
            results[home_team].append(0.5)

    return results

team_results = get_team_results(season_2016)
winloss_df = pd.DataFrame(
    [{"Team": team, "Games": results} for team, results in team_results.items()]
)

winloss_df = (
    winloss_df
    .sort_values("Team")
    .reset_index(drop=True)
    .head(10)
)

Now that we’ve loaded the real-world data, let’s see how we can use gt_plt_winloss().

gt = (
    GT(winloss_df)
    .tab_header(
        title="2016 NFL Season",
    )
    .tab_source_note(
        md(
            '<span style="float: right;">Source: [Lee Sharpe, nflverse](https://github.com/nflverse/nfldata)</span>'
        )
    )
    .cols_align("left", columns="Games")
)

gt.pipe(
    gte.gt_plt_winloss,
    column="Games",
)
2016 NFL Season
Team Games
ARI
ATL
BAL
BUF
CAR
CHI
CIN
CLE
DAL
DEN
Source: Lee Sharpe, nflverse
gt_plt_dumbbell
gt_color_box