Home > database >  how to plot confusion matrix without color coding
how to plot confusion matrix without color coding

Time:03-23

Of all the answers I see on stackoverflow, such as enter image description here

So I want the number shown only.

CodePudding user response:

Use

Complete function:

def plot_confusion_matrix(cm, classes, title,
                          normalize=False,
                          file='confusion_matrix',
                          cmap='gray_r'):
    
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        cm_title = 'Confusion matrix, with normalization'
    else:
        cm_title = title

    fmt = '.3f' if normalize else 'd'
    sns.heatmap(cm, fmt=fmt, annot=True, square=True,
                xticklabels=classes, yticklabels=classes,
                cmap=cmap, vmin=0, vmax=0,
                linewidths=0.75, linecolor='k',
                cbar=False)
    sns.despine(left=False, right=False, top=False, bottom=False)

    plt.title(cm_title)
    plt.ylabel('True class')
    plt.xlabel('Predicted class')
    plt.tight_layout()
    plt.savefig(f'{file}.png')

CodePudding user response:

You can use a ListedColormap with just one color for the colormap. Using heatmap with single color

  • Related