gt-extras
  • Intro
  • Examples
  • API Reference
  1. Colors
  2. gt_data_color_by_group
  • API Reference
  • Plotting
    • gt_plt_bar
    • gt_plt_bar_pct
    • gt_plt_bar_stack
    • gt_plt_bullet
    • gt_plt_conf_int
    • gt_plt_dot
    • gt_plt_dumbbell
    • gt_plt_summary
    • 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
  • Examples
  1. Colors
  2. gt_data_color_by_group

gt_data_color_by_group

gt_data_color_by_group(gt, columns=None, palette=None)

Perform data cell colorization by group.

The gt_data_color_by_group() function takes an existing GT object and adds colors to data cells according to their values within their group (as identified by groupname_col).

Please refer to GT.data_color for more details and examples.

Parameters

columns : SelectExpr = None

The columns to target. Can either be a single column name or a series of column names provided in a list.

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

The color palette to use. This should be a list of colors (e.g., ["#FF0000"◉, "#00FF00"◉, "#0000FF"◉ ]). A ColorBrewer palette could also be used, just supply the name (reference available in the Color palette access from ColorBrewer section). If None, then a default palette will be used.

Examples

from great_tables import GT, md
from great_tables.data import exibble
import gt_extras as gte

gt = (
    GT(exibble, rowname_col="row", groupname_col="group")
    .cols_hide(columns=None)
    .cols_unhide("num")
    .cols_label({"num": "Color by Group"})
    .pipe(gte.gt_duplicate_column, column="num", dupe_name="Color All")
    .tab_source_note(md("Left: `gt_data_color_by_group`, Right: `data_color`"))
)

(
    gt
    .data_color(columns="Color All", palette="PiYG")
    .pipe(gte.gt_data_color_by_group, columns=["num"], palette="PiYG")
)
Color by Group Color All
grp_a
row_1 0.1111 0.1111
row_2 2.222 2.222
row_3 33.33 33.33
row_4 444.4 444.4
grp_b
row_5 5550.0 5550.0
row_6
row_7 777000.0 777000.0
row_8 8880000.0 8880000.0
Left: gt_data_color_by_group, Right: data_color

Notice how in the fourth row, the color is at the green end of the palette because it is the highest in its group when we call gt_data_color_by_group.

gt_color_box
gt_highlight_cols