使用Matplotlib-Cmap-Python打印时获得意外输出

使用Matplotlib-Cmap-Python打印时获得意外输出,python,matplotlib,plot,colormap,Python,Matplotlib,Plot,Colormap,我在我的项目中有一种方法,我验证一个像素是否具有所需的可靠性(就其是否分类为边缘而言),并按照以下方案绘制像素: White -> pixel doesn't have the required reliability Blue -> pixel has the required reliability and it was classified as not edge Red -> pixel has the required reliability and it was c

我在我的项目中有一种方法,我验证一个像素是否具有所需的可靠性(就其是否分类为边缘而言),并按照以下方案绘制像素:

White -> pixel doesn't have the required reliability
Blue -> pixel has the required reliability and it was classified as not edge
Red -> pixel has the required reliability and it was classified as an edge
这是我的代码:

def generate_data_reliability(classification_mean, data_uncertainty, x_axis_label, y_axis_label, plot_title,
                                  file_path, reliability):
        """
        :classification_mean : given a set of images, how was the mean classification for each pixel
        :param data_uncertainty : the uncertainty about the classification
        :param x_axis_label : the x axis label of the data
        :param y_axis_label : the y axis label of the data
        :param plot_title : the title of the data
        :param file_path : the name of the file
        """
        plt.figure()
        # 0 -> certainty
        # 1 -> uncertainty
        r = 0
        b = 0
        w = 0
        has_reliability = numpy.zeros((data_uncertainty.rows, data_uncertainty.cols), float)
        for x, y in product(range(data_uncertainty.rows), range(data_uncertainty.cols)):
            # I the uncertainty is > then the required reliability, doesn't show it
            if data_uncertainty.data[x][y] > (1.0 - reliability):
                has_reliability[x][y] = 0.5
                w += 1
            else:
                has_reliability[x][y] = classification_mean.data[x][y]
                if has_reliability[x][y] == 1.0:
                    r += 1
                if has_reliability[x][y] == 0.0:
                    b += 1

        print reliability, w+r+b, w, r, b

        plt.title(plot_title)
        plt.imshow(has_reliability, extent=[0, classification_mean.cols, classification_mean.rows, 0], cmap='bwr')
        plt.xlabel(x_axis_label)
        plt.ylabel(y_axis_label)
        plt.savefig(file_path + '.png')
        plt.close()
这是我得到的指纹:

>>>> Prewitt
0.8 95100 10329 0 84771
0.9 95100 12380 0 82720
0.99 95100 18577 0 76523
可以看出,随着所需的可靠性越来越高,具有这种可靠性的像素就越少(其中更多的像素将被打印为白色,而没有一个是红色)

但这是我得到的情节:

我不知道为什么,如果我有更少的像素和期望的可靠性,我不会得到更多的白色像素,但这些红色的。我不会改变我的物体,去弄乱它们。哦

我在3个小时左右就被这个问题困住了,不知道出了什么问题

编辑:

在这个cmap0是蓝色的,0.5是白色的,1是红色的,不是吗?我很确定问题是因为我使用的是发散颜色贴图,有时没有中心值。例如,在我在这里发布的情况下,我没有红色值,因此我的值在0.5到1之间变化。然后,matplotlib自动将“最小值”设置为红色,“最大值”设置为蓝色。但我怎么能做到呢?我选择此选项是因为我希望在方案中表示颜色:0=蓝色、0.5=白色和1=红色(我的值始终为0、0.5或1)

任何帮助都将非常感谢


提前谢谢。

好吧,我可以使用自定义颜色贴图实现我想要的效果。代码如下:

@staticmethod
    def generate_data_reliability(classification_mean, data_uncertainty, x_axis_label, y_axis_label, plot_title,
                                  file_path, reliability):
    """
    :param data_uncertainty : the uncertainty about the data
    :param x_axis_label : the x axis label of the data
    :param y_axis_label : the y axis label of the data
    :param plot_title : the title of the data
    :param file_path : the name of the file
    """
    color_map = mpl.colors.ListedColormap(['blue', 'white', 'red'])
    # From 0 to 0.24 -> blue
    # From 0.25 to 0.4 -> white
    # From 0.5 to 1.0 -> red
    bounds = [0.0, 0.25, 0.5, 1.0]
    norm = mpl.colors.BoundaryNorm(bounds, color_map.N)

    plt.figure()
    # 0 -> certainty
    # 1 -> uncertainty
    r = 0
    b = 0
    w = 0
    has_reliability = numpy.zeros((data_uncertainty.rows, data_uncertainty.cols), float)
    for x, y in product(range(data_uncertainty.rows), range(data_uncertainty.cols)):
        # I the uncertainty is > then the required reliability, doesn't show it
        if data_uncertainty.data[x][y] > (1.0 - reliability):
            has_reliability[x][y] = 0.4
        else:
            has_reliability[x][y] = classification_mean.data[x][y]

    plt.title(plot_title)
    plt.imshow(has_reliability, extent=[0, classification_mean.cols, classification_mean.rows, 0],
               interpolation='nearest', cmap=color_map, norm=norm)
    plt.xlabel(x_axis_label)
    plt.ylabel(y_axis_label)
    plt.savefig(file_path + '.png')
    plt.close()

正如您在编辑中提到的,该问题是由颜色栏范围的自动缩放引起的。通过使用调用
imshow()
vmin
vmax
关键字参数,可以强制设置颜色映射的范围

在您的情况下,这将是:

plt.imshow(has_reliability, vmin=0.0, vmax=1.0, extent=[0, classification_mean.cols, classification_mean.rows, 0], cmap='bwr')

这样,数据的范围不会影响颜色贴图的缩放!但是,从长远来看,创建自己的颜色贴图(如您自己的答案中所述)会让您拥有更多的控制权,我认为您提供的示例不会在值的范围内提供渐变(例如,对于0.5到1.0之间的值,默认颜色贴图会以不同的数量混合红色和白色)这可能是你真正想要的

你能发布一个极小的工作示例(包括运行代码的示例图像),以便复制你绘制的图吗?嗨,@three_Pinepples。有一个与9个线程并行运行的项目需要1小时(生成输入图像)。提供代码有点复杂但是我发现了问题所在(见编辑),我只是不知道如何解决它s