3

すべて正規化された4つのhexbinプロットがあります。それらを合計して1つの大きなディストリビューションを作成するにはどうすればよいですか? ここに画像の説明を入力してください 入力ベクトルを連結してから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)

ありがとうございました。

4

1 に答える 1

2

私はあなたが求めていることをするコードを少し書きました。質問のスニペットから、max_vビニング スキームに基づいて分布の高さ ( ) を既に知っているように見えるので、その仮定の下で作業しました。これを適用するデータによっては、これが実際には当てはまらない場合があります。その場合、以下は失敗します (分布の高さの推測/知識と同じくらい良いです)。サンプル データの目的のために、 と の値について (簡単なプロットに基づいて) 妥当な推測を行いましmax_v1max_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使用)、

結果

修正後の結果 (そのままのコード)。

結果

それが役立つことを願っています。

于 2012-08-02T14:20:07.527 に答える