如何提高matplotlib中2D直方图的灵敏度?

时间:2013-10-11 16:39:36

标签: python matplotlib

我构建了一个直方图,反映了matplotlib中细胞的重量。正如您从下面的直方图中看到的那样,颜色区别如此模糊,人们几乎看不到它enter image description here 这可能是因为我选择称重点的方式。

如何增加直方图的“灵敏度”,以便显示高重量区域和低重量区域之间的区别?


修改

根据要求附加的代码:

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。

2 个答案:

答案 0 :(得分:1)

vmin中使用vmaximshow()个参数。

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

例如,您在所有细胞中的最高体重高达500.同时,您的最低体重低至零。然后你的直方图必须从一个颜色极端到另一个反映500差异。这就是为什么区别如此之小。

我建议你的是,虽然我不知道你的问题,但我相信当体重超过一定水平时,比如50,无论是51还是500,都是无关紧要的。所以在{{1}的帮助下}和vmin参数:

vmax

答案 1 :(得分:0)

你尝试过直方图均衡吗?

尝试Histogram Equalization of matplotlib color tables

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

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