How to show numpy NxM array with dtype=float as plain gray scale image?

后端 未结 2 1686
孤街浪徒
孤街浪徒 2021-01-27 14:06

When creating an numpy array with dtype=float, the the presentation method using matplotlib.pyplot.imshow appears to be dependent on the v

相关标签:
2条回答
  • 2021-01-27 14:10

    Found out that imshow does normalize by default, so to avoid this, the vmin and vmax must be given to imshow, which is then:

    plt.imshow(img, cmap='gray', vmin=0, vmax=1)
    
    0 讨论(0)
  • 2021-01-27 14:16

    You have to fix the limits of the color-scale:

    plt.imshow(img, cmap='gray',clim=(0,1))
    

    To get a good feeling of what is going on you could include a colorbar which visualizes the conversion between colors and numerical values; for example using the following code:

    fig,ax = plt.subplots()
    cax = plt.imshow(img, cmap='gray')
    cbar = fig.colorbar(cax)
    plt.show()
    

    Doing this for the two examples immediately makes clear that matplotlib.pyplot updates the range of the color-scale to the data. Consequently the conversion betweens colors and numerical values is different for the two cases.

    0 讨论(0)
提交回复
热议问题