python – 具有固定颜色渐变的np.histogram2D
发布时间:2020-12-20 13:51:10 所属栏目:Python 来源:网络整理
导读:我正在尝试修改现有的 python代码,使用np.histogram2d绘制值的热图.我正在绘制其中的几个,我希望y轴和颜色范围在它们之间具有可比性.我找到了手动设置y_limit的方法,但现在我想要修复颜色范围.以下代码段: hist,xedges,yedges = np.histogram2d(x,y,bins=[2
我正在尝试修改现有的
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.有任何想法吗? 编辑:使用vmin和vmax解决. 解决方法
我从
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.5,1] return np.ma.masked_array(np.interp(value,x,y)) 我用它像: norm = MidpointNormalize(midpoint=0.5,vmin = 0,vmax = 1) ax.scatter(x,c=z,cmap = cm,norm = norm) imshow也有参数规范,它应该有效. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |