pdstools.utils.plot_utils¶
Plot utilities for pdstools visualizations.
This module centralizes the small helpers that the plotting modules across
pdstools all reach for: a typed Figure alias that survives without plotly
installed, an optional-dependency import shim, label abbreviation,
facet-title cleanup, dynamic facet sizing, and report-layout adjustments.
Attributes¶
Functions¶
|
Get the colorscale for a metric. |
|
Truncate a label to |
|
Polars expression equivalent of |
|
Strip the |
|
Resize a faceted plot proportionally to its row count and clean labels. |
|
Clear or set both axis titles in one call. |
|
Apply Quarto-friendly sizing to a figure. |
Module Contents¶
- logger¶
- type Figure = '_Figure'¶
- get_colorscale(metric: str, default: str = 'other') list[tuple[float, str]] | list[str]¶
Get the colorscale for a metric.
- Parameters:
- Returns:
A Plotly-compatible colorscale (list of (position, color) tuples or list of colors).
- Return type:
Examples
>>> get_colorscale("Performance") [(0, '#d91c29'), (0.01, '#F76923'), (0.3, '#20aa50'), (0.8, '#20aa50'), (1, '#0000FF')] >>> get_colorscale("UnknownMetric") ['#d91c29', '#F76923', '#20aa50']
- DEFAULT_LABEL_MAX_LENGTH = 25¶
- abbreviate_label(label: str, max_length: int = DEFAULT_LABEL_MAX_LENGTH, *, from_end: bool = False) str¶
Truncate a label to
max_lengthchars, appending...when shortened.Used for axis tick labels (predictor names, bin symbols, action names) that would otherwise overflow plot containers. Note that truncation is not guaranteed to produce unique labels — callers that require uniqueness must enforce that themselves.
- Parameters:
label (str) – The original label.
max_length (int, optional) – Maximum length before the ellipsis is appended, by default 25.
from_end (bool, optional) – If True, keep the trailing
max_lengthcharacters and prepend...instead of truncating from the end. Useful for action / treatment names whose distinguishing suffix is more informative than the common prefix. Defaults to False.
- Returns:
The original
labelif it fits, otherwise the truncated label with...on the truncated side.- Return type:
- abbreviate_label_expr(column: str | polars.Expr, max_length: int = DEFAULT_LABEL_MAX_LENGTH) polars.Expr¶
Polars expression equivalent of
abbreviate_label().
- simplify_facet_titles(fig: Figure) Figure¶
Strip the
column=prefix from Plotly facet annotations.Plotly Express labels facets as
"<column_name>=<value>"; in our reports the column name is redundant context. This helper rewrites each facet annotation to show only the value.
- fig_update_facet(fig: Figure, n_cols: int = 2, base_height: int = 250, step_height: int = 270) Figure¶
Resize a faceted plot proportionally to its row count and clean labels.
Combines
simplify_facet_titles()with a height calculation that grows the figure as facet rows are added — Plotly Express does not do this on its own, so without this helper faceted plots squash together as more facets are introduced.- Parameters:
- Returns:
The same figure with adjusted
heightand simplified facet titles.- Return type:
- update_axes_clean(fig: Figure, x_title: str | None = '', y_title: str | None = '') Figure¶
Clear or set both axis titles in one call.
A surprising number of plots only need to blank one or both axis titles — this helper replaces the repeated
fig.update_xaxes(title=...).update_yaxes(title=...)pairs.- Parameters:
- Returns:
The same figure (returned for chaining).
- Return type:
- apply_report_layout(fig: Figure, *, n_facets: int = 1, base_height: int = 400, per_facet_height: int = 200, width: int | None = None) Figure¶
Apply Quarto-friendly sizing to a figure.
Quarto HTML reports render charts in a fixed-width container, so charts benefit from a consistent width and a height that scales with the number of facets. Use this in report templates instead of repeating
update_layout(height=..., width=...)calls in every code chunk.- Parameters:
fig (Figure) – The figure to size.
n_facets (int, optional) – Number of facets in the figure (1 for non-faceted), by default 1.
base_height (int, optional) – Height (px) for a single-facet figure, by default 400.
per_facet_height (int, optional) – Additional height (px) per extra facet, by default 200.
width (int | None, optional) – Optional fixed width (px). When
Nonethe figure stays responsive.
- Returns:
The same figure with adjusted
height(andwidthif provided).- Return type:
- HOVER_PERFORMANCE_PCT = 'Performance: %{y:.2%}'¶
- HOVER_SUCCESS_RATE_PCT = 'Success Rate: %{y:.3%}'¶
- HOVER_RESPONSE_COUNT = 'Responses: %{y:,d}'¶
- HOVER_LIFT_PCT = 'Lift: %{x:.2%}'¶