np.histogram2D具有固定的颜色渐变

时间:2015-07-03 08:46:26

标签: python numpy histogram2d

我正在尝试修改现有的python代码,使用np.histogram2d绘制值的热图。我正在绘制其中的几个,我希望y轴和颜色范围在它们之间具有可比性。我找到了手动设置y_limit的方法,但现在我想要修复颜色范围。以下代码段:

    hist,xedges,yedges = np.histogram2d(x,y, bins=[20, 50], range=[ [0, 100.0], [0, y_limit] ])
    # draw the plot
    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1] ]
    im = ax.imshow(hist.T,extent=extent,interpolation='nearest',origin='lower', aspect='auto')
    # colormap, colorbar, labels, ect.
    im.set_cmap('gist_heat_r')
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", "5%", pad="3%")
    ax.figure.colorbar(im, cax=cax)
    ax.set_title(d['name'] + ' foo')
    ax.set_xlabel("bar")
    ax.set_ylabel("coverage")

显示不同颜色范围的示例,其中一个从0到2800以上,另一个从0到2400以上。我希望能够将颜色范围设置为固定的最大值,例如:两者都是3000。有什么想法吗?

enter image description here enter image description here

已编辑:使用vminvmax解决。

1 个答案:

答案 0 :(得分:1)

我从this post

剪下了一段代码
class MidpointNormalize(Normalize):
    def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
    self.midpoint = midpoint
    Normalize.__init__(self, vmin, vmax, clip)

    def __call__(self, value, clip=None):
    # I'm ignoring masked values and all kinds of edge cases to make a
    # simple example...
    x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]
    return np.ma.masked_array(np.interp(value, x, y))

我用它像:

norm = MidpointNormalize(midpoint=0.5,vmin = 0, vmax = 1)
ax.scatter(x, y,c=z, cmap = cm, norm = norm)

imshow也有参数norm,它应该有效。