问题描述
我正在使用
plt.imshow(hist2d, norm = LogNorm(), cmap = gray)
其中 hist2d
是直方图值的矩阵.这工作正常,除了 hist2d
中的元素为零.特别是,我得到以下图像
where hist2d
is a matrix of histogram values. This works fine except for elements in hist2d
that are zero. In particular, I obtain the following image
但希望白色补丁是黑色的.
but would like the white patches to be black.
谢谢!
推荐答案
这是另一种方法,不需要通过为不良像素设置rgb值来处理数据.
Here's an alternative method that does not require you to muck with your data by setting a rgb value for bad pixels.
import copy
data = np.arange(25).reshape((5,5))
my_cmap = copy.copy(matplotlib.cm.get_cmap('gray')) # copy the default cmap
my_cmap.set_bad((0,0,0))
plt.imshow(data,
norm=matplotlib.colors.LogNorm(),
interpolation='nearest',
cmap=my_cmap)
问题在于,带有 0
的bin不能正确地进行日志规范化,因此将它们标记为坏",它们被映射到不同的位置.默认行为是不在这些像素上绘制任何内容.您还可以指定绘制超过或低于颜色图限制的像素的颜色(默认是将它们绘制为最高/最低颜色).
The problem is that bins with 0
can not be properly log normalized so they are flagged as 'bad', which are mapped to differently. The default behavior is to not draw anything on those pixels. You can also specify what color to draw pixels that are over or under the limits of the color map (the default is to draw them as the highest/lowest color).
这篇关于matplotlib.colors.LogNorm中的零问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!