gt-extras
  • Intro
  • API Reference
  1. Plotting
  2. gt_plt_bar_stack
  • 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
  • Note
  1. Plotting
  2. gt_plt_bar_stack

gt_plt_bar_stack

gt_plt_bar_stack(
    gt,
    column,
    labels=None,
    width=100,
    height=30,
    palette=None,
    font_size=10,
    spacing=2,
    num_decimals=0,
    scale_type='relative',
)

Create stacked horizontal bar plots in GT cells.

The gt_plt_bar_stack() function takes an existing GT object and adds stacked horizontal bar charts to a specified column. Each cell displays a series of horizontal bars whose lengths are proportional to the values in the list. The scaling of the bars can be controlled using the scale_type - see below for more info.

Parameters

gt : GT

A GT object to modify.

column : SelectExpr

The column containing lists of numeric values to represent as stacked horizontal bars. Each cell should contain a list of numeric values.

labels : list[str] | None = None

Optional labels for the bars. If provided, these labels will be displayed in the column header, with each label corresponding to a color in the palette.

width : float = 100

The total width of the stacked bar plot in pixels. If scale_type = "absolute", this value will determine the width of the maximum length bar plot.

height : float = 30

The height of the stacked bar plot in pixels.

palette : list[str] | str | None = None

The color palette to use for the bars. This can be a list of colors (e.g., ["#FF0000", "#00FF00", "#0000FF"]) or a named palette (e.g., "viridis"). If None, a default palette will be used.

font_size : int = 10

The font size for the text labels displayed on the bars.

spacing : float = 2

The horizontal gap, in pixels, between each bar. If the spacing is too large relative to the width, a warning will be issued, and no bars will be displayed.

num_decimals : int = 0

The number of decimal places to display in the text labels on the bars.

scale_type : Literal['relative', 'absolute'] = 'relative'

Determines how the bars are scaled. Options are "relative" (bars are scaled relative to the sum of the values in each cell) and "absolute" (bars are scaled relative to the maximum value across all rows).

Returns

: GT

A GT object with stacked horizontal bar plots added to the specified column.

Examples

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

df = pd.DataFrame({
    "x": ["Example A", "Example B", "Example C"],
    "col": [
        [10, 40, 50],
        [30, 30, 40],
        [50, 20, 30],
    ],
})

gt = GT(df)

gt.pipe(
    gte.gt_plt_bar_stack,
    column="col",
    palette=["red", "grey", "black"],
    labels=["Group 1", "Group 2", "Group 3"],
    width=200,
)
x Group 1 | Group 2 | Group 3
Example A
10
40
50
Example B
30
30
40
Example C
50
20
30

If the absolute sum of each row varies, we can treat the rows as portions of a whole.

df = pd.DataFrame({
    "x": ["Example A", "Example B", "Example C"],
    "col": [
        [10, 20, 50],
        [30, 30],
        [50, 10, 10],
    ],
})

gt = GT(df)

gt.pipe(
    gte.gt_plt_bar_stack,
    column="col",
    labels=["Group 1", "Group 2", "Group 3"],
    width=200,
    scale_type="relative",
)
x Group 1 | Group 2 | Group 3
Example A
10
20
50
Example B
30
30
Example C
50
10
10

Or we can treat them as absolute values.

df = pd.DataFrame({
    "x": ["Example A", "Example B", "Example C"],
    "col": [
        [10, 20, 50],
        [30, 30],
        [50, 10, 10],
    ],
})

gt = GT(df)

gt.pipe(
    gte.gt_plt_bar_stack,
    column="col",
    labels=["Group 1", "Group 2", "Group 3"],
    width=200,
    scale_type="absolute",
)
x Group 1 | Group 2 | Group 3
Example A
10
20
50
Example B
30
30
Example C
50
10
10

Note

Values of 0 will not be displayed in the plots.

gt_plt_bar_pct
gt_plt_conf_int