简体   繁体   中英

Display pixels in grayscale without mathplotlib scaling values

I want to plot pixels with values of grayscale 0, 138 and 255 in matplotlib. When in the image there are all pixel values, they are correctly displayed with colors matching their values. But when I display only pixels with values 138 and 0, I can see only black and white colors without grey as if value 138 got mapped to 255. How can I disable it and show white and gray pixels only in one figure?
All pixels diplayed in correct scale
Slice of previous sample where grey color was mapped to white

Code which I'm using to display pixels with incorrect values:

for m in range(len(patterns)):
    axs = plt.subplot(4, math.ceil(len(patterns)/4), m+1)
    axs.imshow(patterns[m].pixels, cmap="gray")
    axs.set_xticks([])
    axs.set_yticks([])
plt.show()

use the vmin and vmax kwargs for imshow to set the min and max of the colormap scale.

For example

axs.imshow(patterns[m].pixels, cmap="gray", vmin=0, vmax=255)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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