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

gt_plt_conf_int

gt_plt_conf_int(
    gt,
    column,
    ci_columns=None,
    ci=0.95,
    width=100,
    dot_color='red',
    border_color='red',
    line_color='royalblue',
    text_color='black',
    text_size='default',
)

Create confidence interval plots in GT cells.

The gt_plt_conf_int() function takes an existing GT object and adds horizontal confidence interval plots to a specified column. Each cell displays a horizontal bar representing the confidence interval, with a dot indicating the mean value. Optionally, the lower and upper confidence interval bounds can be provided directly, or the function can compute them.

If ci_columns is not provided, the function assumes each cell in column contains a list of values and computes the confidence interval using a t-distribution.

Parameters

gt : GT

A GT object to modify.

column : SelectExpr

The column that contains the mean of the sample. This can either be a single number per row, if you have calculated the values ahead of time, or a list of values if you want to calculate the confidence intervals.

ci_columns : SelectExpr | None = None

Optional columns representing the left/right confidence intervals of your sample. If None, the confidence interval will be computed from the data in column using a t-distribution.

ci : float = 0.95

The confidence level to use when computing the interval (if ci_columns is None).

width : float | int = 100

The width of the confidence interval plot in pixels.

dot_color : str = 'red'

The color of the mean dot.

border_color : str = 'red'

The color of the border around the mean dot.

line_color : str = 'royalblue'

The color of the confidence interval bar.

text_color : str = 'black'

The color of the confidence interval labels.

text_size : Literal['small', 'default', 'large', 'largest', 'none'] = 'default'

The size of the text for the confidence interval labels. Options include "none" for no text.

Returns

: GT

A GT object with confidence interval plots added to the specified column.

Examples

import pandas as pd
from great_tables import GT
import gt_extras as gte

df = pd.DataFrame({
    'group': ['A', 'B', 'C'],
    'mean': [5.2, 7.8, 3.4],
    'ci_lower': [3.1, 6.1, 1.8],
    'ci_upper': [7.3, 9.7, 5.0],
    'ci': [5.2, 7.8, 3.4],
})

gt = GT(df)
gt.pipe(
    gte.gt_plt_conf_int,
    column='ci',
    ci_columns=['ci_lower', 'ci_upper'],
    width=120,
)
group mean ci_lower ci_upper ci
A 5.2 3.1 7.3
3.1
7.3
B 7.8 6.1 9.7
6.1
9.7
C 3.4 1.8 5.0
1.8
5

Alternatively we can pass in a column of lists, and the function will compute the CI’s for us.

import numpy as np
np.random.seed(37)

n_per_group = 50
groups = ["A", "B", "C"]
means = [20, 22, 25]
sds = [10, 16, 10]

# Create the data
data = []
for i, (grp, mean, sd) in enumerate(zip(groups, means, sds)):
    values = np.random.normal(mean, sd, n_per_group)
    data.extend([{"grp": grp, "values": val} for val in values])

df_raw = pd.DataFrame(data)
df_summary = (
    df_raw
    .groupby("grp")
    .agg({"values": ["count", "mean", "std", list]})
    .round(3)
)
df_summary.columns = ["n", "avg", "sd", "ci"]

gt = GT(df_summary)
gt.pipe(
    gte.gt_plt_conf_int,
    column="ci",
)
n avg sd ci
50 21.391 9.99
19
24
50 21.719 16.927
16.91
26.53
50 25.528 10.522
23
29

Note

All confidence intervals are scaled to a common range for visual alignment.

gt_plt_dot
gt_highlight_cols