如何在matplotlib中“关闭”imshow()的模糊效果?

47
我希望制作一个概率的彩色图,但是imshow会在概率为零的点周围产生模糊的值。我该如何消除真实网格点周围的这种模糊边缘?
示例:
import numpy as np
import matplotlib.pyplot as plt

a=np.asarray([[  0.00000000e+00 , 1.05824446e-01 ,  2.05086136e-04,   0.00000000e+00],
[  1.05824446e-01 ,  3.15012305e-01  , 1.31255127e-01  , 1.05209188e-01],
 [  2.05086136e-04  , 1.31255127e-01 ,  0.00000000e+00 ,  0.00000000e+00],
 [  0.00000000e+00   ,1.05209188e-01  , 0.00000000e+00  , 0.00000000e+00]])
im=plt.imshow(a,extent=[0,4,0,4],origin='lower',alpha=1,aspect='auto')
plt.show()

在这里输入图片描述

2个回答

79

默认情况下(这在mpl 2.0中有所改变),imshow 对数据进行插值(正如您想要为图像执行的操作)。您只需要告诉它不要插值即可:

默认情况下(自 mpl 2.0 起已更改),imshow 插值数据(正如在处理图像时所需)。您只需要告诉它不要插值即可:

im = plt.imshow(..., interpolation='none')
'nearest'也适用于你想要的情况。请参阅smoothing between pixels of imagesc\imshow in matlab like the matplotlib imshow以获取所有插值类型的示例。
请参阅文档

1
谢谢。正确的属性是插值(interpolation)。 im = plt.imshow(..., interpolation='none') - Cupitor
4
请注意,部分后端不支持“none”,而“nearest”似乎在任何地方都被接受。 - tiago
@tiago 什么后端不接受“none”?这听起来像是一个 bug。 - tacaswell
3
@tcaswell:macosx 后端不支持 interpolation='none',会抛出以下警告:WARNING: The backend (matplotlib.backends.backend_macosx.RendererMac) does not support interpolation='none'. The image will be interpolated with 'nearest' mode. [matplotlib.image] - tiago
它的行为符合预期,只是会引发一个警告? - tacaswell
显示剩余6条评论

3
您也可以使用以下方法:
im = plt.imshow(..., interpolation='nearest')

这对离散变量特别有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接