【问题标题】:np.histogram2D with fixed colour gradient具有固定颜色渐变的 np.histogram2D
【发布时间】:2015-09-21 00:54:53
【问题描述】:

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

    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。有什么想法吗?

已编辑:使用 vminvmax 解决。

【问题讨论】:

标签: python numpy histogram2d


【解决方案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,它应该可以工作。

【讨论】:

  • 完美!如答案中所述,我使用vminvmax 解决了这个问题。
猜你喜欢
  • 2020-10-08
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
  • 2017-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-01
相关资源
最近更新 更多