繁体   English   中英

使用Matplotlib绘图时获得意外的输出-Cmap-Python

[英]Getting unexpected output when plotting with Matplotlib - Cmap - Python

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

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个小时,不知道出什么问题了。

编辑:

在这个cmap中, 0是蓝色,0.5是白色,1是红色,不是吗? 我很确定问题是因为我使用的是发散的颜色图,有时并且没有中心价值。 例如,在我在此处发布的情况下,我没有红色值,因此我的值在0.5到1之间变化。然后,matplotlib自动将我的最小值设置为红色,将最大值设置为蓝色。 但是我该怎么办呢? 我选择这种方式是因为要表示方案中的颜色:0 =蓝色,0.5 =白色和1 =红色(我的值始终为0、0.5或1)。

任何帮助将非常非常感谢。

先感谢您。

正如您在编辑中提到的,问题是由颜色条范围的自动缩放引起的。 您可以通过将vminvmax关键字参数用于对imshow()的调用来imshow()颜色图的范围。

在您的情况下,这将是:

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之间),这可能是您真正想要的!

好吧,我可以使用自定义颜色图实现我想要的。 这是代码:

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

暂无
暂无

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

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