-彩色地图中的Inf——在MATLAB中为低,在Python中为高?

-彩色地图中的Inf——在MATLAB中为低,在Python中为高?,python,colormap,infinity,Python,Colormap,Infinity,我已经将一些MATLAB代码移植到Python中,包括一个绘图实用程序,它可以在2D矩阵(列表)中绘制一些数据的颜色图。MATLAB和Python绘图实用程序非常相似,因此我能够使它们在视觉上非常紧密地匹配,几乎不费吹灰之力 我在这里使用的测试矩阵是: X = [ 1 0 3 ] [ 4 5 6 ] [ 7 8 9 ] MATLAB与测试矩阵 X = [1 0 3; 4 5 6; 7 8 9]; figure(1); imagesc(X); co

我已经将一些MATLAB代码移植到Python中,包括一个绘图实用程序,它可以在2D矩阵(列表)中绘制一些数据的颜色图。MATLAB和Python绘图实用程序非常相似,因此我能够使它们在视觉上非常紧密地匹配,几乎不费吹灰之力

我在这里使用的测试矩阵是:

X = [ 1  0  3 ]
    [ 4  5  6 ]
    [ 7  8  9 ]
MATLAB与测试矩阵

X = [1 0 3;
     4 5 6;
     7 8 9];
figure(1);
imagesc(X);
colormap(hot);
colorbar;
import numpy as np
import matplotlib as plt

X = [ [1,0,3] , [4,5,6] , [7,8,9] ]
fig = []
fig.append( plt.figure(1) )
plt.imshow(X, cmap='hot', interpolation='nearest', aspect='auto')
plt.colorbar()
fig[0].show()
Y = [  0.0000    -Inf        4.7712 ]
    [  6.0206     6.9897     7.7815 ]
    [  8.4510     9.0309     9.5424 ]
Y = 10*log10(X)
figure(2);
imagesc(Y);
colormap(hot);
colorbar;
Y = 10*np.log10(X)
fig.append( plt.figure(2) )
plt.imshow(X, cmap='hot', interpolation='nearest', aspect='auto')
plt.colorbar()
fig[1].show()

带测试矩阵的Python

X = [1 0 3;
     4 5 6;
     7 8 9];
figure(1);
imagesc(X);
colormap(hot);
colorbar;
import numpy as np
import matplotlib as plt

X = [ [1,0,3] , [4,5,6] , [7,8,9] ]
fig = []
fig.append( plt.figure(1) )
plt.imshow(X, cmap='hot', interpolation='nearest', aspect='auto')
plt.colorbar()
fig[0].show()
Y = [  0.0000    -Inf        4.7712 ]
    [  6.0206     6.9897     7.7815 ]
    [  8.4510     9.0309     9.5424 ]
Y = 10*log10(X)
figure(2);
imagesc(Y);
colormap(hot);
colorbar;
Y = 10*np.log10(X)
fig.append( plt.figure(2) )
plt.imshow(X, cmap='hot', interpolation='nearest', aspect='auto')
plt.colorbar()
fig[1].show()

当我转换成dB(取每个元素的log10,乘以10)时,就会出现这个问题,这就给出了dB测试矩阵

X = [1 0 3;
     4 5 6;
     7 8 9];
figure(1);
imagesc(X);
colormap(hot);
colorbar;
import numpy as np
import matplotlib as plt

X = [ [1,0,3] , [4,5,6] , [7,8,9] ]
fig = []
fig.append( plt.figure(1) )
plt.imshow(X, cmap='hot', interpolation='nearest', aspect='auto')
plt.colorbar()
fig[0].show()
Y = [  0.0000    -Inf        4.7712 ]
    [  6.0206     6.9897     7.7815 ]
    [  8.4510     9.0309     9.5424 ]
Y = 10*log10(X)
figure(2);
imagesc(Y);
colormap(hot);
colorbar;
Y = 10*np.log10(X)
fig.append( plt.figure(2) )
plt.imshow(X, cmap='hot', interpolation='nearest', aspect='auto')
plt.colorbar()
fig[1].show()
MATLAB与dB测试矩阵

X = [1 0 3;
     4 5 6;
     7 8 9];
figure(1);
imagesc(X);
colormap(hot);
colorbar;
import numpy as np
import matplotlib as plt

X = [ [1,0,3] , [4,5,6] , [7,8,9] ]
fig = []
fig.append( plt.figure(1) )
plt.imshow(X, cmap='hot', interpolation='nearest', aspect='auto')
plt.colorbar()
fig[0].show()
Y = [  0.0000    -Inf        4.7712 ]
    [  6.0206     6.9897     7.7815 ]
    [  8.4510     9.0309     9.5424 ]
Y = 10*log10(X)
figure(2);
imagesc(Y);
colormap(hot);
colorbar;
Y = 10*np.log10(X)
fig.append( plt.figure(2) )
plt.imshow(X, cmap='hot', interpolation='nearest', aspect='auto')
plt.colorbar()
fig[1].show()

带dB测试矩阵的Python

X = [1 0 3;
     4 5 6;
     7 8 9];
figure(1);
imagesc(X);
colormap(hot);
colorbar;
import numpy as np
import matplotlib as plt

X = [ [1,0,3] , [4,5,6] , [7,8,9] ]
fig = []
fig.append( plt.figure(1) )
plt.imshow(X, cmap='hot', interpolation='nearest', aspect='auto')
plt.colorbar()
fig[0].show()
Y = [  0.0000    -Inf        4.7712 ]
    [  6.0206     6.9897     7.7815 ]
    [  8.4510     9.0309     9.5424 ]
Y = 10*log10(X)
figure(2);
imagesc(Y);
colormap(hot);
colorbar;
Y = 10*np.log10(X)
fig.append( plt.figure(2) )
plt.imshow(X, cmap='hot', interpolation='nearest', aspect='auto')
plt.colorbar()
fig[1].show()

中上元素是怎么回事?它是-Inf,应该被认为是一个低值。在MATLAB中,它被设置为数组中存在的最小值,在这种情况下为0。这是有意义的,因为当-Inf小于0时,如果我们使用它的“实际值”,它将破坏伸缩性

另一方面,Python将这个-Inf值解释为一个高值,将其设置为数组中的最高值——9.5424。如果值只是Inf,那么这对我来说是非常有意义的。但是,它肯定是-Inf,应该是这样的。为什么这里有差异,我能在不影响其他任何事情的情况下解决它吗


编辑:显然,我可以用Inf替换all-Inf,找到矩阵的最小值,并用最小值替换所有Inf。但是,我使用的是大型数据集,因此这样做并保持原始数据完整性不是特别有效。理想情况下,有一种方法可以更改绘图工具解释无限值的方式。

Python不是将
-Inf
绘图作为颜色贴图的最高颜色。它根本不是在策划它。您可以通过切换到没有白色的颜色贴图来确认,如
“酷”

plt.imshow(Y, cmap='cool', interpolation='nearest', aspect='auto')
plt.show()

Python处理图像中的
-Inf
(以及
Inf
NaN
),方法是不渲染该面片,留下轴颜色从后面显示。使用时,MATLAB将
-Inf
(和
NaN
)设置为最低颜色映射值,将
Inf
设置为最高颜色映射值。使用时,MATLAB通过不渲染来处理类似Python的
NaN

看起来您必须将数组中的非有限值替换为有限值才能进行渲染。或者你也可以用一个

如果您只想修复此特定示例(将任何非有限值设置为黑色),可以使用颜色贴图的方法:

cmap=plt.cm.hot
cmap.set_bad('k')
plt.imshow(Y, cmap=cmap, interpolation='nearest', aspect='auto')
plt.show()

这似乎是一个python问题,与matlab无关。这两种实现都可以自由地实现边界情况。考虑删除Matlab标签。我想你的答案已经给出了答案,或者你可能会更改python源代码。我正在寻找一种方法,不必复制服务器上处理的每个庞大数据集,并为绘图增加不必要的计算时间。python是否将其作为颜色图的最高颜色进行绘图,或者只是不绘制它,让背景轴的颜色显示出来?@gnovice很可能是这样的,我没有想到。。。如果是这样的话,我想如果可能的话,改变轴的颜色可以解决这个问题。