[Python] Disable confusion matrix labels

Discussão em 'Python' iniciado por Stack, Junho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I'd like to have a subplot with shared X and Y labels for confusion matrices. That is, the four confusion matrices below will have one "predicted label" at the bottom and one "True label" on the right. I also want to modify the default label size; the matplotlib.rc command does not work on the confusion matrix labels.

    I tried "fig.supxlabel", "set_xlabel", "display_labels" under "ConfusionMatrixDisplay", "set_xticklabels", "matplotlib.RcParams", etc., but none worked. Can anyone help, please?

    import matplotlib.dates as mdates
    import matplotlib

    matplotlib.rc('xtick', labelsize=6)
    matplotlib.rc('ytick', labelsize=6)
    fig, ax = plt.subplots(2, 2, figsize=(8, 8),sharex=True, sharey=True)
    fig.suptitle('Confusion Matrix')

    ax[0,0].set_title("NS Flow", fontsize=8)
    ax[0,1].set_title("Capacity", fontsize=8)
    ax[1,0].set_title("Cost", fontsize=8)
    ax[1,1].set_title("Efficiency", fontsize=8)

    ConfusionMatrixDisplay(
    confusion_matrix=confusion_matrix(y_test, y_pred_binary['NSflow']), display_labels=[False, True]).plot(ax=ax[0,0])

    ConfusionMatrixDisplay(
    confusion_matrix=confusion_matrix(y_test, y_pred_binary['capacity']),display_labels=[False, True]).plot(ax=ax[0,1])

    ConfusionMatrixDisplay(
    confusion_matrix=confusion_matrix(y_test, y_pred_binary['cost']),display_labels=[False, True]).plot(ax=ax[1,0])

    ConfusionMatrixDisplay(
    confusion_matrix=confusion_matrix(y_test, y_pred_binary['efficiency']),display_labels=[False, True]).plot(ax=ax[1,1])

    plt.show()


    [​IMG]

    Continue reading...

Compartilhe esta Página