gt-extras
  • Intro
  • Examples
  • API Reference
  1. Plotting
  2. gt_plt_bullet
  • 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_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_bullet

gt_plt_bullet

gt_plt_bullet(
    gt,
    data_column,
    target_column,
    fill='purple',
    bar_height=20,
    height=30,
    width=60,
    target_color='darkgrey',
    stroke_color='black',
    keep_data_column=False,
)

Create bullet chart plots in GT cells.

The gt_plt_bullet() function takes an existing GT object and adds bullet chart visualizations to compare actual values against target values. Each bullet chart consists of a horizontal bar representing the actual value and a vertical line indicating the target value, making it easy to assess performance against goals or benchmarks.

Parameters

gt : GT

A GT object to modify.

data_column : SelectExpr

The column containing the actual values to be plotted as horizontal bars.

target_column : SelectExpr

The column containing the target values to be displayed as vertical reference lines. This column will be automatically hidden from the returned table.

fill : str = 'purple'

The fill color for the horizontal bars representing actual values.

bar_height : float = 20

The height of each horizontal bar in pixels.

height : float = 30

The height of the bullet chart plot in pixels. This allows for spacing around the bar and target line.

width : float = 60

The width of the maximum bar in pixels. Bars are scaled proportionally to this width.

target_color : str = 'darkgrey'

The color of the vertical target line.

stroke_color : str | None = 'black'

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

keep_data_column : bool = False

Whether to keep the original data 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 bullet chart plots added to the specified data column. The target column is automatically hidden from the table.

Examples

import polars as pl
from great_tables import GT
from great_tables.data import airquality
import gt_extras as gte

air_bullet = (
    pl.from_pandas(airquality)
    .with_columns(pl.col("Temp").mean().over("Month").alias("target_temp"))
    .group_by("Month", maintain_order=True)
    .head(2)
    .with_columns(
        (pl.col("Month").cast(pl.Utf8) + "/" + pl.col("Day").cast(pl.Utf8)).alias(
            "Date"
        )
    )
    .select(["Date", "Temp", "target_temp"])
    .with_columns(pl.col(["Temp", "target_temp"]).round(1))
)

(
    GT(air_bullet, rowname_col="Date")
    .tab_header(title="Daily Temp vs Monthly Average")
    .tab_source_note("Target line shows monthly average temperature")

    ## Call gt_plt_bullet
    .pipe(
        gte.gt_plt_bullet,
        data_column="Temp",
        target_column="target_temp",
        width=200,
        fill="tomato",
        target_color="darkblue",
        keep_data_column=True,
    )
    .cols_move_to_end("Temp")
    .cols_align("left", "Temp plot")
)
Daily Temp vs Monthly Average
Temp plot Temp
5/1
67
5/2
72
6/1
78
6/2
74
7/1
84
7/2
85
8/1
81
8/2
81
9/1
91
9/2
92
Target line shows monthly average temperature

Note

Both data and target values are scaled to a common domain for consistent visualization. The scaling domain is automatically determined as [0, max(data_values, target_values)].

gt_plt_bar_stack
gt_plt_conf_int