plt.imshow无法显示全1的矩阵

问题描述 投票:0回答:1

这里是一个简单的可复制代码:

    import numpy as np
    import matplotlib.pyplot as plt

    plt.figure()
    img = np.ones((3,3))
    plt.imshow(img)
    plt.figure()
    plt.imshow(np.array([1,1,1,1,0,1,1,1,1]).reshape(3,3))

它将为您提供如下图像:

image of matrix of 1

enter image description here

即使您使用

plt.imshow(img.astype(float)) or
plt.imshow(img*255)

仍然是同一张图片...我只需要matplotlib给我第一张图像作为黄色图像,就像第二张图像一样...我需要帮助!

matplotlib imshow
1个回答
0
投票

明确使用vminvmaximshow参数:

当使用标量数据并且没有明确的normvminvmax时,定义色图所覆盖的数据范围。默认情况下,颜色图覆盖所提供数据的完整值范围。如果使用norm参数,则会忽略vminvmax

imshow

对于第二个示例,数据范围是import numpy as np import matplotlib.pyplot as plt plt.figure() img = np.ones((3, 3)) plt.imshow(img, vmin=0, vmax=1) plt.figure() plt.imshow(np.array([1, 1, 1, 1, 0, 1, 1, 1, 1]).reshape(3, 3)) plt.show() ,因此默认情况下,颜色是相对于该范围缩放的。但是,对于第一个示例,无法从数据本身(全部)中提取所需的[0 ... 1]数据范围,因此您必须明确提供该信息。

希望有帮助!

[0 ... 1]
© www.soinside.com 2019 - 2024. All rights reserved.