问题描述
我想绘制概率的彩色图,但是 imshow 会为概率为零的点生成模糊值.如何摆脱真实网格点周围的模糊边缘?
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?
示例:
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()
推荐答案
默认情况下(在 mpl 2.0 中已更改),imshow
会插入数据(就像您希望对图像执行的操作一样).您需要做的就是告诉它不要插值:
By default (which is changed mpl 2.0), imshow
interpolates the data (as you would want to do for an image). All you need to do is tell it to not interpolate:
im = plt.imshow(..., interpolation='none')
'nearest'
也可以满足您的需求.请参阅 像素之间的平滑在 matlab 中的 imagescimshow 就像 matplotlib imshow 的所有类型的插值的例子.
'nearest'
will also work for what you want. See smoothing between pixels of imagescimshow in matlab like the matplotlib imshow for examples of all of the kinds of interpolation.
这篇关于如何在 matplotlib 中“关闭"imshow() 的模糊效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!