python - 使用 imshow 显示图像 - Matplotlib/Python

标签 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

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

如果能帮助我澄清这一点,我将不胜感激。

提前谢谢您。

最佳答案

它正在按照您的指示进行操作:

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

关于python - 使用 imshow 显示图像 - Matplotlib/Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22546431/

相关文章:

python - matplotlib 中有两个变量的直方图

python - 将 Xticks 频率设置为数据帧索引

java - 更改Android中EditText的颜色

python - 为什么 Python Matplotlib 条形图的 X 轴刻度显示奇怪和错误的负值?

python - Django 休息 : Auth user is not passed to the serialiser error - Field is required

python - 在循环中使用字母表作为计数器

CSS 更改 Logo 悬停颜色

Jquery 交替行颜色在悬停功能后似乎不起作用

python - 如何触发模型更新它链接到的列表

python - 我如何以编程方式注销用户?[Django]