Python imshow(img, cmap=cm.gray) 显示 128 值的白色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12760797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
imshow(img, cmap=cm.gray) shows a white for 128 value
提问by Norfeldt
I'm moving from MatLab to python and playing around with the imshow function.
我正在从 MatLab 转移到 python 并使用 imshow 函数。
I can't seem to get my head around why it doesn't show the value 128 as grey with I have chosen the cmap to be gray-scale.
我似乎无法理解为什么它没有将值 128 显示为灰色,因为我选择了 cmap 为灰度。


It seems as it uses the grayscale for highest (128) and lowest values.. I want it to use the grayscale for [0:255]. How do I do that?
似乎它使用最高 (128) 和最低值的灰度..我希望它使用 [0:255] 的灰度。我怎么做?
采纳答案by unutbu
Use the vminand vmaxparameters:
使用vmin和vmax参数:
plt.imshow(bg, cmap=plt.get_cmap('gray'), vmin=0, vmax=255)
Without specifying vminand vmax, plt.imshowauto-adjusts its range to the min and max of the data.
不指定vminand vmax,将plt.imshow其范围自动调整为数据的最小值和最大值。
I do not know of a way to set default vminand vmaxparameters for all imshow plots, but you could use functools.partialto prepare a custom imshow-like command with default parameters set:
我不知道有什么方法可以为所有 imshow 图设置默认值vmin和vmax参数,但是您可以使用functools.partial设置了默认参数的自定义 imshow 类命令:
import matplotlib.pyplot as plt
import numpy as np
import functools
bwimshow = functools.partial(plt.imshow, vmin=0, vmax=255,
cmap=plt.get_cmap('gray'))
dots = np.random.randn(10, 10)*255
bwimshow(dots)
cbar = plt.colorbar()
plt.show()

