Forest Plot
A forest plot shows per-item point estimates and confidence intervals as horizontal bars, one row per item (subgroup, variable, study, …). The x-axis uses a log scale with natural-scale tick labels, making the chart suited for hazard ratios, odds ratios, and risk ratios.
A solid vertical rule marks the null effect (null_value, default 1.0).
Example
import polars as pl
from plotutils.forest import plot_forest
df = pl.DataFrame({
"subgroup": ["Overall", "Age < 65", "Age ≥ 65", "Male", "Female"],
"hr": [0.78, 0.72, 0.85, 0.80, 0.75],
"low": [0.62, 0.55, 0.65, 0.61, 0.57],
"high":[0.98, 0.94, 1.11, 1.05, 0.99],
})
chart = plot_forest(
df,
center_col="hr",
low_col="low",
high_col="high",
label_col="subgroup",
x_title="Hazard Ratio",
)
With minimum-effect boundaries and colour grouping
Pass min_effect to draw symmetric dashed rules at
null_value / min_effect and null_value * min_effect — the boundary of a
minimum clinically meaningful effect. color_col maps a categorical column
to colour.
chart = plot_forest(
df,
center_col="hr",
low_col="low",
high_col="high",
label_col="subgroup",
min_effect=1.25, # draws lines at 0.80 and 1.25
color_col="population",
title="Treatment effect by subgroup",
)
Reference
plotutils.forest.plot_forest(df, center_col, low_col, high_col, label_col, *, null_value=1.0, min_effect=None, title='', width=400, height=None, color_col=None, sort_col=None, ascending=True, x_title=None, y_title=None)
Forest plot with log-scale x-axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Data with one row per item. Must contain |
required |
center_col
|
str
|
Column with the point estimate (e.g. hazard ratio). |
required |
low_col
|
str
|
Column with the lower bound of the confidence interval. |
required |
high_col
|
str
|
Column with the upper bound of the confidence interval. |
required |
label_col
|
str
|
Column with the item label shown on the y-axis. Row order in df is
preserved ( |
required |
null_value
|
float
|
x-position of the "no effect" vertical rule. Use |
1.0
|
min_effect
|
float or None
|
When provided, two dashed rules are drawn at
|
None
|
title
|
str
|
Chart title. |
''
|
width
|
int
|
Chart width in pixels. |
400
|
height
|
int or None
|
Chart height in pixels. When |
None
|
color_col
|
str or None
|
Optional column used to colour CI bars and center points. When
provided an |
None
|
sort_col
|
str or None
|
Column to sort rows by. When |
None
|
ascending
|
bool
|
Sort direction when |
True
|
x_title
|
str or None
|
X-axis title. Defaults to |
None
|
y_title
|
str or None
|
Y-axis title. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
LayerChart
|
|
Source code in src/plotutils/forest.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |