Deviations
Visualize how individual measurements deviate from their per-group mean. This removes the "level" effect and focuses purely on scatter and variability, making it easy to compare consistency across conditions that have very different absolute values.
Basic usage
import polars as pl
from plotutils.uncertainty import plot_deviations
df = pl.DataFrame({
"category": ["Low"] * 10 + ["Medium"] * 10 + ["High"] * 10,
"value": [1.0, 0.8, 1.2, 0.9, 1.1, 1.0, 0.7, 1.3, 0.95, 1.05,
2.5, 2.3, 2.7, 2.4, 2.6, 2.5, 2.2, 2.8, 2.45, 2.55,
4.0, 3.8, 4.2, 3.9, 4.1, 4.0, 3.7, 4.3, 3.95, 4.05],
})
chart = plot_deviations(df, x_col="category", y_col="value")
Each point shows y - mean(y) for its group. The horizontal line at zero is the group mean reference.
Relative deviations
Use relative=True to express deviations as a fraction of the group mean —
(y - mean) / mean. This is useful when comparing groups with very different
magnitudes, since the y-axis becomes dimensionless:
Tolerance bands
Add symmetric reference lines with add_levels to mark acceptable deviation
thresholds. For example, add_levels=[0.1, 0.2] draws lines at ±0.1 and ±0.2:
df = pl.DataFrame({
"x": ["A"] * 10 + ["B"] * 10,
"y": [1.0, 1.1, 0.9, 1.2, 0.8, 1.0, 1.1, 0.9, 1.05, 0.95,
2.0, 2.1, 1.9, 2.2, 1.8, 2.0, 2.1, 1.9, 2.05, 1.95],
})
chart = plot_deviations(
df,
x_col="x",
y_col="y",
add_levels=[0.1, 0.2],
)
Reference
plotutils.uncertainty.plot_deviations(df, x_col, y_col, title='', relative=False, add_levels=None, x_labels=None, scale_type='linear')
Create a plot showing deviations of y values from their per-group mean.
Computes y - mean(y) per x group. When relative is True, computes
(y - mean(y)) / mean(y) instead. A horizontal line at 0 is always
drawn. Additional symmetric level lines (e.g. tolerance bands) can be
added via add_levels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Raw data with multiple y values per x category. |
required |
x_col
|
str
|
Column for x-axis (categorical or numeric). |
required |
y_col
|
str
|
Column for y values. |
required |
title
|
str
|
Plot title. |
''
|
relative
|
bool
|
If True, deviations are divided by the group mean. |
False
|
add_levels
|
list[float] or None
|
Extra horizontal levels drawn symmetrically at +level and -level. |
None
|
x_labels
|
dict[float, str] or None
|
Mapping of numeric x values to custom labels (enables quantitative x-axis with labelled ticks). |
None
|
scale_type
|
str
|
Scale type for the x-axis: "linear" or "log". |
'linear'
|
Source code in src/plotutils/uncertainty.py
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 | |