简体   繁体   English

matplotlib在混淆矩阵中绘制固定颜色

[英]matplotlib plotting fixed colors in confusion matrix

Rather than using a colormap, is there a way to manually plot fixed colors into a confusion matrix? 除了使用颜色图之外,还有没有办法将固定颜色手动绘制到混乱矩阵中? I'm trying to color the tiles green, red, yellow, and black for the top-left, top-right, bottom-left, and bottom-right respectively. 我正在尝试分别将左上角,右上角,左下角和右下角的瓷砖颜色分别为绿色,红色,黄色和黑色。 Here's the code I have thus far: 到目前为止,这是我的代码:

def plot_confusion_matrix( cm_raw, title="Confusion Matrix", cmap=colors.ListedColormap( [
                       "lightgreen", "mistyrose", "lightyellow", "lightgray"] ) ):
    cm = cm_raw.astype( "float" ) / cm_raw.sum()

    plt.imshow( cm, interpolation="nearest", cmap=cmap )
    class_names = ["Negative", "Positive"]
    plt.title( title )
    plt.xlabel( "Predicted Label" )
    plt.ylabel( "True Label" )
    tick_marks  = np.arange( len( class_names ) )

    s = [["True Negative", "False Positive"], ["False Negative", "True Positive"]]
    for row_index in range( 2 ):
        for col_index in range( 2 ):
            plt.text( col_index, row_index, str( s[row_index][col_index]) + ":\n" + str( format(
            cm[row_index][col_index] * 100, ".2f" ) ) + "%", ha="center", color="black" )
    x_listsum = cm.sum( axis=0 ) * 100
    x_sum     = ["{0:.2f}%".format( x_index ) for x_index in x_listsum]
    y_listsum = cm.sum( axis=1 ) * 100
    y_sum     = ["{0:.2f}%".format( y_index ) for y_index in y_listsum]
    plt.xticks( tick_marks, x_sum )
    plt.yticks( tick_marks, y_sum )

    plt.show()

And here is the current output: 这是当前输出:

Confusion matrix 混淆矩阵

If you provide a Minimal, Complete, and Verifiable example, there is a chance that someone will help you do that. 如果您提供了一个最小,完整和可验证的示例,则可能有人会帮助您完成此操作。 Else what can be said is that you need to create the values for your colors. 可以说的是,您需要为颜色创建值。 Eg the pixels that should get the first color of the 4-color colormap, would be set to 0, those for the second to 0.33, for the third 0.66 and 1 for the last. 例如,应该获得4色颜色图的第一种颜色的像素将设置为0,将第二种颜色的像素设置为0.33,第三种颜色的像素设置为0.66,最后一个为1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM