问题描述
我正从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)
plt.imshow
在未指定vmin
和vmax
的情况下,将其范围自动调整为数据的最小值和最大值.
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值显示白色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!