matplotlib imshow-默认颜色归一化 [英] matplotlib imshow - default colour normalisation
问题描述
使用imshow
时,我的颜色映射始终存在问题,某些颜色似乎只是变成黑色.我终于意识到,imshow
在默认情况下似乎可以归一化我给出的浮点值矩阵.
I have consistently had problems with my colour maps when using imshow
, some colours seem to just become black. I have finally realised that imshow
seems to, by default, normalise the matrix of floating point values I give it.
我本来希望像[[0,0.25],[0.5,0.75]]
这样的数组显示对应于那些绝对值的映射中的适当颜色,但是0.75将被解释为1.在极端情况下,N x N数组为0.2 (例如),只会产生一个大的黑色方块,而不是在颜色映射中期望0.2对应的任何地方(可能是20%的灰色).
I would have expected an array such as [[0,0.25],[0.5,0.75]]
to display the appropriate colours from the map, corresponding to those absolute values but the 0.75 will be interpreted as a 1. In the extreme case, an N x N array of 0.2 (for example), would just produce one big black square, rather than whatever one would expect 0.2 to correspond to in the colour map (perhaps a 20% grey).
是否有防止这种行为的方法?当自定义颜色图具有许多不连续性时,这尤其令人烦恼,比例的微小变化可能会导致所有颜色完全改变.
Is there a way to prevent this behaviour? It is particularly annoying when custom colour maps have many discontinuities, a small change in scale could cause all the colours to completely change.
推荐答案
只需指定vmin=0, vmax=1
.
默认情况下,imshow
将数据标准化为最小值和最大值.您可以使用vmin
和vmax
参数或使用norm
参数(如果要进行非线性缩放)来控制它.
By default, imshow
normalizes the data to its min and max. You can control this with either the vmin
and vmax
arguments or with the norm
argument (if you want a non-linear scaling).
作为一个简单的例子:
import matplotlib.pyplot as plt
data = [[0, 0.25], [0.5, 0.75]]
fig, ax = plt.subplots()
im = ax.imshow(data, cmap=plt.get_cmap('hot'), interpolation='nearest',
vmin=0, vmax=1)
fig.colorbar(im)
plt.show()
这篇关于matplotlib imshow-默认颜色归一化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!