【问题标题】:matplotlib plotting fixed colors in confusion matrixmatplotlib 在混淆矩阵中绘制固定颜色
【发布时间】:2018-07-12 00:50:59
【问题描述】:

有没有办法手动将固定颜色绘制到混淆矩阵中,而不是使用颜色图?我正在尝试将左上角、右上角、左下角和右下角的瓷砖分别涂成绿色、红色、黄色和黑色。这是我到目前为止的代码:

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()

这是当前的输出:

Confusion matrix

【问题讨论】:

  • 您将使用颜色图绘制固定颜色。所以不清楚这要求什么。
  • This 链接可能有助于学习如何设置颜色图。
  • 我为造成的混乱道歉。我正在尝试将每个图块着色为特定颜色(即左上角:绿色,右上角:红色等),而不会根据我的值更改颜色,如果这有意义的话。
  • 如果您提供minimal reproducible example,则有可能有人会帮助您这样做。否则可以说的是您需要为颜色创建值。例如。应该获得 4 色颜色图的第一种颜色的像素将设置为 0,将第二个设置为 0.33,第三个设置为 0.66,最后一个设置为 1。

标签: python matplotlib


【解决方案1】:

如果您提供一个最小、完整且可验证的示例,那么有人可能会帮助您做到这一点。否则可以说的是您需要为颜色创建值。例如。应该获得 4 色颜色图的第一种颜色的像素将设置为 0,将第二个设置为 0.33,第三个设置为 0.66,最后一个设置为 1。

【讨论】:

    猜你喜欢
    • 2016-09-27
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2021-07-21
    • 2016-06-04
    相关资源
    最近更新 更多