anndict.plot.plot_confusion_matrix

anndict.plot.plot_confusion_matrix#

anndict.plot.plot_confusion_matrix(true_labels_encoded, predicted_labels_encoded, label_encoder, color_map, title='Confusion Matrix', row_color_keys=None, col_color_keys=None, true_label_color_dict=None, predicted_label_color_dict=None, figsize=None, diagonalize=False, true_ticklabels=None, predicted_ticklabels=None, annot=None)[source]#

Plot a confusion matrix using cluster mapping with optional color annotations.

This function computes a confusion matrix based on encoded true and predicted labels and optionally reorders the matrix to maximize diagonal alignment. It supports row and column coloring based on custom dictionaries and color maps.

Parameters:
true_labels_encoded ndarray

Encoded true labels.

predicted_labels_encoded ndarray

Encoded predicted labels.

label_encoder LabelEncoder

A fitted sklearn LabelEncoder that can decode the labels.

color_map dict[str, dict[str, str]]

A nested dictionary specifying how labels should be colored. The first-level keys match possible color keys (e.g., 'row', 'col'), and each maps to a dict from label attributes to color codes.

title str (default: 'Confusion Matrix')

The title of the plot.

row_color_keys list[str] | None (default: None)

Keys to extract color information for the rows from color_map.

col_color_keys list[str] | None (default: None)

Keys to extract color information for the columns from color_map.

true_label_color_dict dict[str, dict[str, str]] | None (default: None)

A nested dict specifying row-label-specific color mappings.

predicted_label_color_dict dict[str, dict[str, str]] | None (default: None)

A nested dict specifying column-label-specific color mappings.

true_labels

Original (not encoded) true labels, if needed.

predicted_labels

Original (not encoded) predicted labels, if needed.

figsize tuple[float, float] | None (default: None)

Figure size passed to seaborn’s clustermap().

diagonalize bool (default: False)

If True, reorders the confusion matrix to maximize its diagonal alignment.

true_ticklabels bool | list[str] | None (default: None)

Labels to display along the rows of the confusion matrix. If True, display all; if False, display none.

predicted_ticklabels bool | list[str] | None (default: None)

Labels to display along the columns of the confusion matrix. If True, display all; if False, display none.

annot bool | None (default: None)

Whether to annotate each cell with its numeric value.

Return type:

ClusterGrid

Returns:

A seaborn ClusterGrid object displaying the plotted confusion matrix.

Notes

  • If the number of true or predicted labels exceeds 40,

    tick labels and annotations are disabled by default for better visibility.

  • The matrix is normalized by the number of samples in each true class.

  • If diagonalize is True, reorder the columns and

    row to diagonalize the matrix to the extent possible.

Examples

# Assume true_encoded, pred_encoded, and le are given, along with a color_map dict
g = plot_confusion_matrix(
    true_labels_encoded=true_encoded,
    predicted_labels_encoded=pred_encoded,
    label_encoder=le,
    color_map={'some_color_key': {'some_label_value': '#ff0000'}}
)