用imshow显示图像 - Matplotlib / Python

时间:2014-03-20 22:23:49

标签: python colors matplotlib imshow

我试图用imshow绘制图像,但是我得到了我没想到的输出...显示我的图像的方法是:

def generate_data_black_and_white_heat_map(data, x_axis_label, y_axis_label, plot_title, file_path):
        plt.figure()
        plt.title(plot_title)
        plt.imshow(data.data, extent=[0, data.cols, data.rows, 0], cmap='Greys')
        plt.xlabel(x_axis_label)
        plt.ylabel(y_axis_label)
        plt.savefig(file_path + '.png')
        plt.close()

我的数据表示为:

def __init__(self, open_image=False):
        """
        The Data constructor
        """
        self.data = misc.lena() / 255.0
        x, y = self.data.shape
        self.rows = x
        self.cols = y

我做了一些计算,在某些时候我必须这样做:

# A -> 2D ndarray
A.data[A.data >= 0.5] = 1.0
A.data[A.data < 0.5] = 0.0

这给了我:

enter image description here

但我想要相反(白色背景)。所以,我刚刚这样做了:

# A -> 2D ndarray
A.data[A.data >= 0.5] = 0.0
A.data[A.data < 0.5] = 1.0

然后,我得到了这个(!!!):

enter image description here

我只是没有得到它。这对我来说没有任何意义。奇怪的是,如果我这样做的原因:

for x in range(A.cols):
        for y in range(A.rows):
            if A.data[x][y] >= 0.5:
                A.data[x][y] = 0.0
            else:
                A.data[x][y] = 1.0

有效。我是以错误的方式访问某些内容吗?

非常感谢任何在我脑海中澄清这一点的帮助。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

这正是你要告诉它的事情:

A[A >= 0.5] = 0.0  #  all of you values are now < 0.5
A[A < 0.5] = 1.0   # all of your values are now 1

只做

要好得多
B = A > .5  # true (1) where above thershold
iB = B < .5 # true (1) where below threshold