Python matplotlib imshow - 默认颜色归一化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22121239/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:17:01  来源:igfitidea点击:

matplotlib imshow - default colour normalisation

pythonmatplotlibnormalizationimshow

提问by oLas

I have consistently had problems with my colour maps when using imshow, some colours seem to just become black. I have finally realised that imshowseems to, by default, normalise the matrix of floating point values I give it.

使用时我的颜色图一直有问题imshow,有些颜色似乎变成黑色。我终于意识到,imshow默认情况下,似乎对我给它的浮点值矩阵进行了归一化。

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).

我本来期望一个数组,例如[[0,0.25],[0.5,0.75]]显示地图中适当的颜色,对应于那些绝对值,但 0.75 将被解释为 1。在极端情况下,0.2 的 N x N 数组(例如),将只需生成一个大的黑色方块,而不是在颜色图中对应于 0.2 的任何内容(可能是 20% 的灰色)。

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.

有没有办法防止这种行为?当自定义颜色映射有许多不连续性时,尤其令人讨厌,比例的微小变化可能会导致所有颜色完全改变。

采纳答案by Joe Kington

Just specify vmin=0, vmax=1.

只需指定vmin=0, vmax=1.

By default, imshownormalizes the data to its min and max. You can control this with either the vminand vmaxarguments or with the normargument (if you want a non-linear scaling).

默认情况下,imshow将数据标准化为其最小值和最大值。您可以使用vminvmax参数或使用norm参数(如果您想要非线性缩放)来控制它。

As a quick example:

举个简单的例子:

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()

enter image description here

在此处输入图片说明