gt-extras
  • Get Started
  • API Reference
  1. Colors
  2. gt_hulk_col_numeric
  • API Reference
  • Plotting
    • gt_plt_bar
    • gt_plt_dot
    • gt_plt_conf_int
  • Colors
    • gt_highlight_cols
    • gt_hulk_col_numeric
    • gt_color_box
  • Themes
    • gt_theme_538
    • gt_theme_espn
    • gt_theme_guardian
    • gt_theme_nytimes
    • gt_theme_excel
    • gt_theme_dot_matrix
    • gt_theme_dark
    • gt_theme_pff
  • Icons and Images
    • fa_icon_repeat
    • gt_fa_rating
  • HTML Helpers
    • gt_hyperlink
    • with_tooltip

On this page

  • Parameters
  • Returns
  • Examples
  1. Colors
  2. gt_hulk_col_numeric

gt_hulk_col_numeric

gt_hulk_col_numeric(
    gt,
    columns=None,
    palette='PRGn',
    domain=None,
    na_color=None,
    alpha=None,
    reverse=False,
    autocolor_text=True,
)

Apply a color gradient to numeric columns in a GT object.

The gt_hulk_col_numeric() function takes an existing GT object and applies a color gradient to the background of specified numeric columns, based on their values. This is useful for visually emphasizing the distribution or magnitude of numeric data within a table.

Parameters

gt : GT

An existing GT object.

columns : SelectExpr = None

The columns to target. Can be a single column name or a list of column names. If None, the color gradient is applied to all columns.

palette : str | list[str] = 'PRGn'

The color palette to use for the gradient. Can be a string referencing a palette name or a list of color hex codes. Defaults to the "PRGn" palette from the ColorBrewer package.

domain : list[int] | list[float] | None = None

The range of values to map to the color palette. Should be a list of two values (min and max). If None, the domain is inferred from the data.

na_color : str | None = None

The color to use for missing (NA) values. If None, a default color is used.

alpha : int | float | None = None

The alpha (transparency) value for the colors, as a float between 0 (fully transparent) and 1 (fully opaque).

reverse : bool = False

If True, reverses the color palette direction.

autocolor_text : bool = True

If True, automatically adjusts text color for readability against the background.

Returns

: GT

The modified GT object, allowing for method chaining.

Examples

from great_tables import GT
from great_tables.data import gtcars
import gt_extras as gte

gtcars_mini = gtcars.loc[0:8, ["model", "mfr", "year", "hp", "trq", "mpg_h"]]

gt = (
    GT(gtcars_mini, rowname_col="model")
    .tab_stubhead(label="Car")
)

gt.pipe(gte.gt_hulk_col_numeric, columns=["hp", "trq", "mpg_h"])
Car mfr year hp trq mpg_h
GT Ford 2017 647.0 550.0 18.0
458 Speciale Ferrari 2015 597.0 398.0 17.0
458 Spider Ferrari 2015 562.0 398.0 17.0
458 Italia Ferrari 2014 562.0 398.0 17.0
488 GTB Ferrari 2016 661.0 561.0 22.0
California Ferrari 2015 553.0 557.0 23.0
GTC4Lusso Ferrari 2017 680.0 514.0 17.0
FF Ferrari 2015 652.0 504.0 16.0
F12Berlinetta Ferrari 2015 731.0 509.0 16.0

A more involved setup.

from great_tables.data import towny

towny_mini = towny[
    [
        "name",
        "pop_change_1996_2001_pct",
        "pop_change_2001_2006_pct",
        "pop_change_2006_2011_pct",
        "pop_change_2011_2016_pct",
        "pop_change_2016_2021_pct",
    ]
].head(10)

gt = (
    GT(towny_mini, rowname_col="name")
    .tab_stubhead(label="Town")
    .tab_spanner(
        label="Population Change",
        columns=[1, 2, 3, 4, 5]
    )
    .cols_label(
        pop_change_1996_2001_pct="1996-2001",
        pop_change_2001_2006_pct="2001-2006",
        pop_change_2006_2011_pct="2006-2011",
        pop_change_2011_2016_pct="2011-2016",
        pop_change_2016_2021_pct="2016-2021",
    )
)

gt.pipe(gte.gt_hulk_col_numeric, columns=[1, 2, 3, 4, 5], domain = [-0.1, 0.23])
Town Population Change
1996-2001 2001-2006 2006-2011 2011-2016 2016-2021
Addington Highlands -0.0111 0.0458 0.002 -0.0791 0.0932
Adelaide Metcalfe 0.0067 -0.0044 -0.0341 -0.0125 0.007
Adjala-Tosorontio 0.0773 0.0608 -0.0086 0.0351 0.0013
Admaston/Bromley -0.0046 -0.0382 0.0471 0.032 0.0204
Ajax 0.1447 0.2226 0.2155 0.0919 0.0584
Alberton -0.0691 0.0021 -0.0981 0.1215 -0.0155
Alfred and Plantagenet 0.0334 0.0071 0.0626 0.0526 0.0278
Algonquin Highlands 0.083 0.0816 0.1063 0.0755 0.1008
Alnwick/Haldimand 0.0575 0.1008 0.0283 0.0381 0.0879
Amaranth 0.0928 0.0199 0.0307 0.0293 0.0608
gt_highlight_cols
gt_color_box