问题描述
我正在从 MatLab 转向 python 并尝试使用 imshow 函数.
I'm moving from MatLab to python and playing around with the imshow function.
我似乎无法理解为什么它没有将值 128 显示为灰色,因为我选择了 cmap 为灰度.
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) 和最低值使用灰度.我希望它对 [0:255] 使用灰度.我该怎么做?
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?
推荐答案
使用 vmin
和 vmax
参数:
plt.imshow(bg, cmap=plt.get_cmap('gray'), vmin=0, vmax=255)
不指定vmin
和vmax
,plt.imshow
会自动将其范围调整为数据的最小值和最大值.
Without specifying vmin
and vmax
, plt.imshow
auto-adjusts its range to the min and max of the data.
我不知道有什么方法可以为所有 imshow 图设置默认的 vmin
和 vmax
参数,但您可以使用 functools.partial
准备一个自定义的类似 imshow 的命令,设置默认参数:
I do not know of a way to set default vmin
and vmax
parameters for all imshow plots, but you could use functools.partial
to prepare a custom imshow-like command with default parameters set:
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()
这篇关于imshow(img, cmap=cm.gray) 显示 128 值的白色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!