python imshow灰度静态颜色值 [英] python imshow grayscale static color values
问题描述
我了解使用matplotlib.pyplot的imshow为我提供了一个不错的草图,可用于可视化矩阵.我的问题是,当我想可视化矩阵时,该函数根据我传递的值来调整颜色密度. 例如:
I understand using matplotlib.pyplot's imshow gives me a nice sketch that can be used to visualize matrices. My question is that when I want to visualize a matrix, the function adjusts the color density according to the values I am passing. for example:
#define a numpy matrix with values between 0 and 1
k=numpy.array([
[ 1. , 0.9701425 , 0.99931483],
[ 0.9701425 , 1. , 0.97845444],
[ 0.99931483, 0.97845444, 1. ]])
#plot the matrix
plt.imshow(k,cmap=cm.gist_gray)
我得到的图像与1和.97之间有很大的差异.如果我做类似的事情:
I get an image with a huge difference between 1 and .97 . if I do something like:
k[2][2]=.1
plt.imshow(k,cmap=cm.gist_gray)
我得到了完全不同的图片.有什么办法解决这个问题?有什么方法可以使图像具有静态颜色值,而不是像上面那样动态更改颜色值?
I get a totally different picture. Is there any way to tackle this problem? is there any way to have an image where we have static color values instead of dynamically changing ones as above?
推荐答案
You can use the vmin
and vmax
keyword arguments of imshow
as documented here. In particular, if you modify your imshow
call to
plt.imshow(k, vmin=0, vmax=1)
将对颜色进行归一化,就好像数据中存在值0
和值1
.
the colours will be normalised as if there was a value 0
and a value 1
present in the data.
这篇关于python imshow灰度静态颜色值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!