Plotting Module

Visualization tools for calibration analysis.

This module provides functions for creating diagnostic plots to assess and visualize the calibration quality of probabilistic predictions. All plots are generated using Plotly for interactive visualization.

mcgrad.plotting.plot_global_calibration_curve(data, score_col, label_col, num_bins=40, sample_weight_col=None, binning_method='equispaced', plot_incomplete_cis=True, x_lim=(0, 1.1))[source]

Plots a global calibration curve with confidence intervals and score histogram.

The calibration curve shows the relationship between predicted scores and actual label proportions. Calibrated bins (where the diagonal falls within the confidence interval) are shown in blue, while miscalibrated bins are shown in red.

Parameters:
  • data (DataFrame) – DataFrame containing scores and labels.

  • score_col (str) – Column name for the predicted scores.

  • label_col (str) – Column name for the binary labels.

  • num_bins (int) – Number of bins for the calibration curve.

  • sample_weight_col (str | None) – Optional column name for sample weights.

  • binning_method (Literal['equispaced', 'equisized']) – Method for binning scores. Either “equispaced” (equal-width bins) or “equisized” (equal-count bins).

  • plot_incomplete_cis (bool) – Whether to plot bins with incomplete confidence intervals (i.e., bins with NaN values).

  • x_lim (Tuple[float, float]) – Tuple specifying both the x-axis and y-axis limits (same range applied to both axes).

Return type:

Figure

Returns:

A Plotly Figure object with the calibration curve, confidence intervals, and a histogram of scores.

mcgrad.plotting.plot_calibration_curve_by_segment(data, group_var, score_col, label_col, num_bins=20, n_cols=4, sample_weight_col=None, binning_method='equispaced')[source]

Plots calibration curves for each segment defined by a grouping variable.

Creates a grid of subplots, one per unique value of the grouping variable. Each subplot shows a calibration curve with confidence intervals. Calibrated bins are shown in blue, miscalibrated bins in red.

Parameters:
  • data (DataFrame) – DataFrame containing scores, labels, and the grouping variable.

  • group_var (str) – Column name for the grouping variable that defines segments.

  • score_col (str) – Column name for the predicted scores.

  • label_col (str) – Column name for the binary labels.

  • num_bins (int) – Number of bins for each calibration curve.

  • n_cols (int) – Number of columns in the subplot grid.

  • sample_weight_col (str | None) – Optional column name for sample weights.

  • binning_method (Literal['equispaced', 'equisized']) – Method for binning scores. Either “equispaced” (equal-width bins) or “equisized” (equal-count bins).

Return type:

Figure

Returns:

A Plotly Figure object with a grid of calibration curve subplots.

mcgrad.plotting.plot_segment_calibration_errors(mce, highlight_feature=None, quantity='segments_ecce_relative')[source]

Plots a segment-level calibration error scatter plot.

This visualization displays the specified calibration quantity against segment size, helping to assess calibration across different data segments defined by categorical and numerical features.

Parameters:
  • mce (MulticalibrationError) – A MulticalibrationError object containing computed segment-level metrics.

  • highlight_feature (str | None) – Optional feature name to color-code points by.

  • quantity (Literal['segments_ecce_relative', 'segments_ecce', 'segments_ecce_pvalue', 'segments_ecce_sigma']) –

    The quantity to plot. Options:

    • segments_ecce_relative: ECCE as percentage of prevalence (default)

    • segments_ecce: Absolute ECCE values

    • segments_ecce_pvalue: P-values for calibration test

    • segments_ecce_sigma: ECCE in standard deviations

Return type:

Figure

Returns:

A Plotly Figure object with the scatter plot of the specified quantity against segment size.

mcgrad.plotting.plot_learning_curve(mcgrad_model, show_all=False)[source]

Plots a learning curve for an MCGrad model.

Parameters:
  • mcgrad_model (MCGrad) – An MCGrad model object.

  • show_all (bool) – Whether to show all metrics in the learning curve. If False, only the metric specified in the model’s early_stopping_score_func is shown.

Return type:

Figure

Returns:

A Plotly Figure object representing the learning curve.