Python 将hexbin图添加到一起

Python 将hexbin图添加到一起,python,matplotlib,Python,Matplotlib,我有四个hexbin图,它们都被标准化了。我如何将它们加在一起,形成一个大的分布? 我曾尝试连接输入向量,然后创建hexbin图,但这会导致各个分布的标准化: 那么,在保持行业标准化的同时,如何添加单独的hexbin分布呢 我的代码的相关部分是: def hex_plot(x,y,max_v): bounds = [0,max_v*m.exp(-(3**2)/2),max_v*m.exp(-2),max_v*m.exp(-0.5),max_v] # The sigma bounds

我有四个hexbin图,它们都被标准化了。我如何将它们加在一起,形成一个大的分布? 我曾尝试连接输入向量,然后创建hexbin图,但这会导致各个分布的标准化: 那么,在保持行业标准化的同时,如何添加单独的hexbin分布呢

我的代码的相关部分是:

def hex_plot(x,y,max_v):
  bounds = [0,max_v*m.exp(-(3**2)/2),max_v*m.exp(-2),max_v*m.exp(-0.5),max_v]   # The sigma bounds
  norm = mpl.colors.BoundaryNorm(bounds, ncolors=4)
  hex_ = plt.hexbin(x, y, C=None, gridsize=gridsize,reduce_C_function=np.mean,cmap=cmap,mincnt=1,norm=norm)
  print "Hex plot max: ",hex_.norm.vmax
  return hex_

gridsize=50
cmap = mpl.colors.ListedColormap(['grey','#6A92D4','#1049A9','#052C6E'])

hex_plot(x_tot,y_tot,34840)

谢谢。

我已经编写了一些代码,可以满足您的要求。从你问题中的片段中,看起来你已经知道了给定装箱方案的发行版的高度(
max_v
),所以我就是在这个假设下工作的。根据您所应用的数据,实际情况可能并非如此,在这种情况下,以下操作将失败(这取决于您对分布高度的猜测/了解)。出于示例数据的目的,我刚刚对
max_v1
max_v2
的值进行了合理的猜测(基于快速绘图)。切换我为注释版本定义的
c1
c2
应该会重现您原来的问题

import scipy
import matplotlib.pyplot as pyplot
import matplotlib.colors
import math

#need to know the height of the distributions a priori
max_v1 = 850 #approximate height of distribution 1 (defined below) with binning defined below
max_v2 = 400 #approximate height of distribution 2 (defined below) with binning defined below
max_v = max(max_v1,max_v2)

#make 2 differently sized datasets (so will require different normalizations)
#all normal distributions with assorted means/variances
x1 = scipy.randn(50000)/6.0+0.5
y1 = scipy.randn(50000)/3.0+0.5
x2 = scipy.randn(100000)/2.0-0.5
y2 = scipy.randn(100000)/2.0-0.5
#c1 = scipy.ones(len(x1)) #I don't assign meaningful weights here
#c2 = scipy.ones(len(x2)) #I don't assign meaningful weights here
c1 = scipy.ones(len(x1))*(max_v/max_v1) #highest distribution: no net change in normalization here
c2 = scipy.ones(len(x2))*(max_v/max_v2) #renormalized to same height as highest distribution

#define plot boundaries
xmin=-2.0
xmax=2.0
ymin=-2.0
ymax=2.0

#custom colormap
cmap = matplotlib.colors.ListedColormap(['grey','#6A92D4','#1049A9','#052C6E'])

#the bounds of 1sigma, 2sigma, etc. regions
bounds = [0,max_v*math.exp(-(3**2)/2),max_v*math.exp(-2),max_v*math.exp(-0.5),max_v]
norm = matplotlib.colors.BoundaryNorm(bounds, ncolors=4)

#make the hexbin plot
normalized = pyplot
hexplot = normalized.subplot(111)
normalized.hexbin(scipy.concatenate((x1,x2)), scipy.concatenate((y1,y2)), C=scipy.concatenate((c1,c2)), cmap=cmap, mincnt=1, extent=(xmin,xmax,ymin,ymax),gridsize=50, reduce_C_function=scipy.sum, norm=norm) #combine distributions and weights
hexplot.axis([xmin,xmax,ymin,ymax])
cax = pyplot.axes([0.86, 0.1, 0.03, 0.85])
clims = cax.axis()
cb = normalized.colorbar(cax=cax)
cax.set_yticklabels([' ','3','2','1',' '])
normalized.subplots_adjust(wspace=0, hspace=0, bottom=0.1, right=0.78, top=0.95, left=0.12)

normalized.show()
这里是没有修复的(注释了
c1
c2
已使用),以及有修复的(按原样编码);(似乎我的低代表阻止我发布实际图像)

希望有帮助