2017-05-25 16 views
1

私の質問は、私の2つのbinnedプロットを相関させ、Pearsonの相関係数を出力する方法です。Python(numpy) - 2つのbinnedプロットを関連付ける

np.corrcoef機能に必要なビニングされた配列を正しく抽出する方法がわかりません。ここに私のスクリプトは次のとおりです。

import numpy as np 
import matplotlib.pyplot as plt 

A = np.genfromtxt('data1.txt') 
x1 = A[:,1] 
y1 = A[:,2] 

B=np.genfromtxt('data2.txt') 
x2 = B[:,1] 
y2 = B[:,2] 

fig = plt.figure() 
plt.subplots_adjust(hspace=0.5) 
plt.subplot(121) 
AA = plt.hexbin(x1,y1,cmap='jet',gridsize=500,vmin=0,vmax=450,mincnt=1) 
plt.axis([-180,180,-180,180]) 
cb = plt.colorbar() 
plt.title('Data1') 

plt.subplot(122) 
BB = plt.hexbin(x2,y2,cmap='jet',gridsize=500,vmin=0,vmax=450,mincnt=1) 
plt.axis([-180,180,-180,180]) 
cb = plt.colorbar() 
plt.title('Data 2') 

array1 = np.ndarray.flatten(AA) 
array2 = np.ndarray.flatten(BB) 

print np.corrcoef(array1,array2) 

plt.show() 

答えて

1

答えがdocumentationで見つけることができます:

戻り値:オブジェクト

PolyCollectionインスタンス。このPolyCollectionget_array()を使用して、各六角形のカウントを取得します。

A = np.genfromtxt('data1.txt') 
x1 = A[:,1] 
y1 = A[:,2] 

B = np.genfromtxt('data2.txt') 
x2 = B[:,1] 
y2 = B[:,2] 

# make figure and axes 
fig, (ax1, ax2) = plt.subplots(1, 2) 

# define common keyword arguments 
hex_params = dict(cmap='jet', gridsize=500, vmin=0, vmax=450, mincnt=1) 

# plot and set titles 
hex1 = ax1.hexbin(x1, y1, **hex_params) 
hex2 = ax2.hexbin(x2, y2, **hex_params) 
ax1.set_title('Data 1') 
ax2.set_title('Data 2') 

# set axes lims 
[ax.set_xlim(-180, 180) for ax in (ax1, ax2)] 
[ax.set_ylim(-180, 180) for ax in (ax1, ax2)] 

# add single colorbar 
fig.subplots_adjust(right=0.8, hspace=0.5) 
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7]) 
fig.colorbar(hex2, cax=cbar_ax) 

# get binned data and corr coeff 
binned1 = hex1.get_array() 
binned2 = hex2.get_array()  
print np.corrcoef(binned1, binned2) 

plt.show() 

二つのコメントかかわら:

ここ

はあなたのコードの改訂版だあなたは、ピアソンの相関係数をよろしいですか?実際に何を見せようとしていますか?配布物が同じか異なっていることを示したい場合は、Kolmogorov-Smirnovテストを使用することができます。

また、カラーマップとしてjetを使用しないでください。 Jet is bad

+0

スクリプトに感謝します。また、KSテストを提案してくれてありがとうございます。それは、配布を比較するより厳しい方法です。 – EA00

関連する問題