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

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

How to 'turn off' blurry effect of imshow() in matplotlib?

pythonnumpymatplotlibplot

提问by Cupitor

I want to make a color plot of probabilities however imshow generates blurry values for points which have zero probability. How can I get rid of this blurry periphery around real grid points?

我想绘制概率的彩色图,但是 imshow 会为概率为零的点生成模糊值。如何摆脱真实网格点周围的模糊边缘?

Example:

例子:

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

enter image description here

在此处输入图片说明

采纳答案by tacaswell

By default (which is changed mpl 2.0), imshowinterpolates the data (as you would want to do for an image). All you need to do is tell it to not interpolate:

默认情况下(已更改为 mpl 2.0),imshow插入数据(就像您希望对图像执行的操作一样)。您需要做的就是告诉它不要插值:

im = plt.imshow(..., interpolation='none')

'nearest'will also work for what you want. See smoothing between pixels of imagesc\imshow in matlab like the matplotlib imshowfor examples of all of the kinds of interpolation.

'nearest'也将适用于您想要的。有关所有类型的插值的示例,请参阅matlab 中的 imagesc\imshow 像素之间的平滑,如 matplotlib imshow

doc

文档

回答by apublius

You may also use:

您还可以使用:

im = plt.imshow(..., interpolation='nearest')

This works especially well for discrete variables.

这对于离散变量特别有效。