gt-extras
  • Get Started
  • API Reference
  1. Plotting
  2. gt_plt_bar
  • 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
  • Note
  1. Plotting
  2. gt_plt_bar

gt_plt_bar

gt_plt_bar(
    gt,
    columns=None,
    fill='purple',
    bar_height=20,
    height=30,
    width=60,
    stroke_color='black',
    scale_type=None,
    scale_color='white',
    domain=None,
)

Create horizontal bar plots in GT cells.

The gt_plt_bar() function takes an existing GT object and adds horizontal bar charts to specified columns. Each cell value is represented as a horizontal bar with length proportional to the cell’s numeric value relative to the column’s maximum value.

Parameters

gt : GT

A GT object to modify.

columns : SelectExpr = None

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

fill : str = 'purple'

The fill color for the bars.

bar_height : int = 20

The height of each individual bar in pixels.

height : int = 30

The height of the bar plot in pixels.

width : int = 60

The width of the maximum bar in pixels

stroke_color : str | None = 'black'

The color of the vertical axis on the left side of the bar. The default is black, but if None is passed no stroke will be drawn.

scale_type : Literal['percent', 'number'] | None = None

The type of value to show on bars. Options are "number", "percent", or None for no labels.

scale_color : str = 'white'

The color of text labels on the bars (when scale_type is not None).

Returns

: GT

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

Examples

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

gtcars_mini = gtcars.loc[
    9:17,
    ["model", "mfr", "year", "hp", "hp_rpm", "trq", "trq_rpm", "mpg_c", "mpg_h"]
]

gt = (
    GT(gtcars_mini, rowname_col="model")
    .tab_stubhead(label="Car")
    .cols_align("center")
    .cols_align("left", columns="mfr")
)

gt.pipe(
    gte.gt_plt_bar,
    columns= ["hp", "hp_rpm", "trq", "trq_rpm", "mpg_c", "mpg_h"]
)
Car mfr year hp hp_rpm trq trq_rpm mpg_c mpg_h
LaFerrari Ferrari 2015
NSX Acura 2017
GT-R Nissan 2016
Aventador Lamborghini 2015
Huracan Lamborghini 2015
Gallardo Lamborghini 2014
Continental GT Bentley 2016
Granturismo Maserati 2016
Quattroporte Maserati 2016

Note

Each column’s bars are scaled independently based on that column’s min/max values.

API Reference
gt_plt_dot