plots.plot_outcome_distribution_by_category
plots.plot_outcome_distribution_by_category(
categories,
prediction_sets,
y_true,
*,
top_n=None,
min_count=None,
other_label='Other',
sort_by='count',
ascending=False,
category_order=None,
normalize=True,
show_counts=True,
count_rotation=0,
show_title=True,
title=None,
category_name='Category',
figsize=(12, 6),
colors=None,
rotate_xticks=45,
legend_loc='upper right',
legend_bbox_to_anchor=None,
show_legend=True,
ax=None,
)Plot stacked bar chart showing outcome distribution by categorical variable.
This visualization shows how conformal prediction outcomes (SC, SI, TS0, TS1) are distributed across different categories. Each bar represents a category, with colors indicating the proportion of each outcome.
Parameters
categories : array-like of shape (n_samples,)-
Categorical variable values.
prediction_sets : list of sets or lists-
Conformal prediction sets, one per sample.
y_true : array-like of shape (n_samples,)-
True binary labels (0 or 1).
top_n : int = None-
Only show top N categories by count. Other categories are collapsed into a single “Other” category.
min_count : int = None-
Only show categories with at least this many samples. Others are collapsed into “Other”.
other_label : str = "Other"-
Label for collapsed categories. Set to None to exclude them entirely.
sort_by : (count, sc_rate, si_rate, alpha) = "count"-
How to sort categories: - “count”: By sample count - “sc_rate”: By SC proportion (highest first if ascending=False) - “si_rate”: By SI proportion - “alpha”: Alphabetically
ascending : bool = False-
Sort order. False means descending (highest first).
category_order : list of str = None-
Explicit ordering of bars. When provided, listed categories appear in the given order regardless of
sort_by; categories present in the data but not in the list are appended afterwards in the order produced bysort_by. Use this for ordinal categorical variables (e.g.emp_length). AUserWarningis emitted when none of the listed categories are found in the data (likely a typo). normalize : bool = True-
If True, bars sum to 1 within each category (proportions). If False, show raw counts.
show_counts : bool = True-
If True, annotate bars with sample count per category.
count_rotation : int = 0-
Rotation angle (degrees) for the
n=<count>annotations on top of each bar. Useful when there are many categories and horizontal labels overlap. When non-zero, the y-axis upper limit is automatically enlarged to accommodate the rotated text. show_title : bool = True-
If False, no title is drawn (the
titleargument is ignored). title : str = None-
Plot title. If None, uses a default based on category_name.
category_name : str = "Category"-
Name of the categorical variable for axis labels.
figsize : tuple = (12, 6)-
Figure size.
colors : dict = None-
Custom colors for outcomes.
rotate_xticks : int = 45-
Rotation angle for x-axis labels.
legend_loc : str = "upper right"-
Matplotlib
locstring for the legend (e.g."upper left","lower center"). legend_bbox_to_anchor : tuple = None-
If set, passed to
ax.legend(bbox_to_anchor=...)— typically used withlegend_loc="upper left"and(1.02, 1.0)to place the legend outside the axes on the right. show_legend : bool = True-
If False, the outcome legend is omitted entirely.
ax : matplotlib.axes.Axes = None-
Axes to plot on. If None, creates a new figure.
Returns
fig : matplotlib.figure.Figure-
The figure object.
ax : matplotlib.axes.Axes-
The axes object.
category_stats : pandas.DataFrame-
DataFrame with outcome counts/proportions per category.
See Also
plot_outcome_distribution_by_bins : For numeric features. conformalpy.outcomes.categorize_outcomes : Compute outcomes from prediction sets.
Examples
>>> from conformalpy.plots import plot_outcome_distribution_by_category
>>> fig, ax, stats = plot_outcome_distribution_by_category(
... X_test["home_ownership"].values,
... prediction_sets,
... y_test,
... category_name="Home Ownership",
... top_n=5
... )>>> # Sort by SC rate to find best-performing categories
>>> fig, ax, stats = plot_outcome_distribution_by_category(
... X_test["purpose"].values,
... prediction_sets,
... y_test,
... sort_by="sc_rate",
... ascending=False,
... top_n=10
... )Notes
Interpreting the plot:
Categories with high SC rates indicate segments where the model performs well and predictions can be trusted. Categories with high SI rates may need additional features or specialized models.
For P2P lending categorical variables:
- Home ownership: Own/mortgage often has higher SC (more financial stability)
- Loan purpose: Debt consolidation may show different patterns than small business loans
- Employment length: Longer employment often correlates with higher SC