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

gt_plt_bar

gt_plt_bar(
    gt,
    columns=None,
    fill='purple',
    bar_height=20,
    height=30,
    width=60,
    stroke_color='black',
    show_labels=False,
    label_color='white',
    domain=None,
    keep_columns=False,
)

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 or a list of columns (by name or index). If None, the bar plot is applied to all numeric columns.

fill : str = 'purple'

The fill color for the bars.

bar_height : float = 20

The height of each individual bar in pixels.

height : float = 30

The height of the bar plot in pixels. In practice, this allows for the bar to appear less stout, the larger the difference between height and bar_height.

width : float = 60

The width of the maximum bar in pixels. Not all bars will have this width.

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.

show_labels : bool = False

Whether or not to show labels on the bars.

label_color : str = 'white'

The color of text labels on the bars (when show_labels is True).

keep_columns : bool = False

Whether to keep the original column values. If this flag is True, the plotted values will be duplicated into a new column with the string ” plot” appended to the end of the column name. See gt_duplicate_column() for more details.

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_bar_pct