Boxplots & Strip Plots
Doubly-grouped distribution plots for one continuous variable against two binary outcomes. Two chart variants are provided:
- Boxplot (
plot_bivariate_boxes) — side-by-side boxplots for each (label_x, label_y) combination. - Strip plot (
plot_bivariate_strip) — jittered strip plot showing every individual sample, coloured by label_y.
Example data
import polars as pl
df = pl.DataFrame({
"score": [0.8, 0.6, 0.9, 0.7, 0.5, 0.3, 0.4, 0.2, 0.85, 0.65, 0.35, 0.25],
"outcome_a": [1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0],
"outcome_b": [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
"patient_id": [f"P{i:03d}" for i in range(12)],
})
Boxplot
from plotutils.boxplot import plot_bivariate_boxes
chart = plot_bivariate_boxes(
df,
score_col="score",
label_x_col="outcome_a",
label_y_col="outcome_b",
)
Strip plot
from plotutils.boxplot import plot_bivariate_strip
chart = plot_bivariate_strip(
df,
score_col="score",
label_x_col="outcome_a",
label_y_col="outcome_b",
id_col="patient_id",
)
Missing scores
When some patients have missing scores, pass missing_score_df to show them
as cross marks below the main chart area:
missing_df = pl.DataFrame({
"outcome_a": [1, 0, 1],
"outcome_b": [0, 1, 0],
"patient_id": ["M001", "M002", "M003"],
})
chart = plot_bivariate_boxes(
df,
score_col="score",
label_x_col="outcome_a",
label_y_col="outcome_b",
id_col="patient_id",
missing_score_df=missing_df,
)
Reference
plotutils.boxplot.plot_bivariate_boxes(df, score_col, label_x_col, label_y_col, title='', width=340, height=300, y_title=None, x_title=None, color_title=None, id_col=None, missing_score_df=None)
Doubly-grouped boxplot: score vs two binary outcomes.
The outer grouping (x-axis) is label_x_col; the inner grouping
(side-by-side boxes within each outer group) is label_y_col.
This produces four boxes — one per (label_x, label_y) combination —
making it easy to read off the marginal and joint effects of both
outcomes on the variable's distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Raw data with score and binary label columns. |
required |
score_col
|
str
|
Column with the continuous variable. |
required |
label_x_col
|
str
|
First binary outcome column (outer x-axis grouping). |
required |
label_y_col
|
str
|
Second binary outcome column (inner grouping via color / xOffset). |
required |
title
|
str
|
Chart title. |
''
|
width
|
int
|
Chart dimensions in pixels. |
340
|
height
|
int
|
Chart dimensions in pixels. |
340
|
y_title
|
str or None
|
Y-axis title. Defaults to |
None
|
x_title
|
str or None
|
X-axis title. Defaults to |
None
|
color_title
|
str or None
|
Legend title for the color encoding. Defaults to |
None
|
id_col
|
str or None
|
Optional column name containing patient / sample identifiers. When provided, a transparent point layer is added on top of the boxes so that hovering over an individual data point reveals its ID. |
None
|
missing_score_df
|
DataFrame or None
|
Optional DataFrame of patients with missing scores (containing
|
None
|
Returns:
| Type | Description |
|---|---|
Chart or LayerChart
|
|
Source code in src/plotutils/boxplot.py
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 | |
plotutils.boxplot.plot_bivariate_strip(df, score_col, label_x_col, label_y_col, title='', width=340, height=300, y_title=None, x_title=None, color_title=None, jitter_seed=0, id_col=None, missing_score_df=None)
Jittered strip plot: score vs two binary outcomes.
Each sample is drawn as a semi-transparent circle. Within each
label_x_col band the points are spread horizontally: the two
label_y_col sub-groups are offset to opposite sides of the band
centre (±15 px by default), and an additional small random jitter
(±7 px) reduces overplotting within each sub-group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Raw data with score and binary label columns. |
required |
score_col
|
str
|
Column with the continuous variable. |
required |
label_x_col
|
str
|
First binary outcome column (outer x-axis grouping). |
required |
label_y_col
|
str
|
Second binary outcome column (inner color grouping). |
required |
title
|
str
|
Chart title. |
''
|
width
|
int
|
Chart dimensions in pixels. |
340
|
height
|
int
|
Chart dimensions in pixels. |
340
|
y_title
|
str or None
|
Y-axis title. Defaults to |
None
|
x_title
|
str or None
|
X-axis title. Defaults to |
None
|
color_title
|
str or None
|
Legend title for the color encoding. Defaults to |
None
|
jitter_seed
|
int
|
Seed for the random horizontal jitter (reproducible renders). |
0
|
id_col
|
str or None
|
Optional column name containing patient / sample identifiers. When provided, the ID appears in the tooltip on mouseover. |
None
|
missing_score_df
|
DataFrame or None
|
Optional DataFrame of patients with missing scores (containing
|
None
|
Returns:
| Type | Description |
|---|---|
Chart
|
|
Source code in src/plotutils/boxplot.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |