3

我已经构建了一个直方图,反映了 matplotlib 中单元格的权重。从下面的直方图中可以看出,颜色区分非常模糊,人们几乎看不到它在此处输入图像描述 这可能是因为我选择了权衡点的方式。

如何提高直方图的“灵敏度”,以便它可以显示高权重区域和低权重区域之间的区别?


编辑

按要求附上的代码:

def generateFreqMap(trajectories, pointWeightLists, representatives):
    # these three lists are all in a one-to-one correpondance
    xOfAllPoints = [point[0] for trajectory in trajectories for point in trajectory]
    yOfAllPoints = [point[1] for trajectory in trajectories for point in trajectory]
    weightsOfAllPoints =[pointWeight for pointWeightList in pointWeightLists for pointWeight in pointWeightList]
    leftBound, rightBound, topBound, bottomBound = min(xOfAllPoints), max(xOfAllPoints), max(yOfAllPoints), min(yOfAllPoints)
    # parameters for histogram
    x_edges = np.linspace(int(mt.floor(leftBound)), int(mt.ceil(rightBound)), int(mt.ceil(rightBound))-int(mt.floor(leftBound))+1)
    y_edges = np.linspace(int(mt.floor(bottomBound)), int(mt.ceil(topBound)), int(mt.ceil(topBound))-int(mt.floor(bottomBound))+1)
    # construct the histogram
    wcounts = np.histogram2d(xOfAllPoints, yOfAllPoints, bins=(x_edges, y_edges), normed=False, weights=weightsOfAllPoints)[0]
    # wcounts is a 2D array, with each element representing the weighted count in a bins
    # show histogram
    extent = x_edges[0], x_edges[-1], y_edges[0], y_edges[-1]
    imshow(np.transpose(wcounts), extent=extent, alpha=0.5, cmap=cm.summer) # alpha controls the transparency
    plt.xlabel('x (m)')
    plt.ylabel('y (m)')
    plt.title('Histogram of %i Trajectories'%TRAJECTORY_NUMBER);
    savefig(PROJECT_PATH + '\\data\\%i_histogram.svg'%len(trajectories))
    return wcounts

第 i 个点的权重为 0.995^i。所以第 1 点的权重最大,为 1。

4

2 回答 2

1

中的使用vminvmax参数imshow()

这里的问题是你的动态范围太宽了。我敢打赌,如果您在直方图旁边显示彩条,您就会明白为什么会立即发生这种情况。由于我无法复制您的实验,因此我将对此进行一些猜测。

例如,您在所有单元格中的最高体重高达 500。同时,您的最低体重低至零。然后你的直方图必须从它的一种颜色极端到另一个反映 500 差异的颜色。这就是差异如此之小的原因。

我建议你的是,虽然我不知道你的问题,但我相信当体重超过一定水平时,比如说50,是51还是500无所谓。所以在vminvmax参数的帮助下:

imshow(np.transpose(wcounts), extent=extent, alpha=0.5, cmap=cm.summer, vmin=0, **vmax=50**) # alpha controls the transparency
于 2013-10-16T01:51:28.740 回答
0

您是否尝试过直方图均衡?

尝试matplotlib 颜色表的直方图均衡

我刚刚在我的目录中为测试图像运行了上面的代码。它很好地展示了细节。我在这里复制并粘贴了代码。

import pylab
import matplotlib.colors
import numpy

im = pylab.imread(inputFile).sum(axis=2) # make grayscale
pylab.imshow(im, cmap=pylab.cm.gray)
pylab.title('orig')
imvals = numpy.sort(im.flatten())
lo = imvals[0]
hi = imvals[-1]
steps = (imvals[::len(imvals)/256] - lo) / (hi - lo)
num_steps = float(len(steps))
interps = [(s, idx/num_steps, idx/num_steps) for idx, s in enumerate(steps)]
interps.append((1, 1, 1))
cdict = {'red' : interps,
         'green' : interps,
         'blue' : interps}
histeq_cmap = matplotlib.colors.LinearSegmentedColormap('HistEq', cdict)
pylab.figure()
pylab.imshow(im, cmap=histeq_cmap)
pylab.title('histeq')
pylab.show()
于 2013-10-15T20:05:18.800 回答