2016-04-08 8 views
0

2つの変数がイベントの発生に及ぼす影響を視覚化するために、matplotlibを使用して2dヒストグラムをプロットします。イベントの2Dヒストグラムが、イベント確率のx軸とy軸の1D棒グラフと、pythonとmatplotlibを使用して一致しない

私のテストケースでは、イベントは「実現したい」であり、変数xは落ちる星の数であり、yは妖精のゴッドマザーの関与です。私がしたいのは、落ちる星や妖精の大怪獣の箱に当てはまる2dヒストグラムのヒストグラムをプロットすることです。そして、各軸の隣に、落ちる星と妖精の魔女のビン(イベントごとのヒストグラムビンの確率を含む1D棒グラフ)ごとに、イベント/(イベント+非イベント)の願いが成立する確率を示したいと思います。棒グラフのビンは、2dのヒストグラムのビンに対応していなければなりません。しかし、バーチャートとヒストグラムビンの間にはわずかなずれがあるようです。

棒グラフを正しく整列させるには、最初と最後のビンエッジに対応する軸のリミットの設定が有効ですか?これらの制限が設定されたら、インデックスとは対照的に、軸上の位置としてビンの中心をplt.bar()に供給できますか?次のように

マイコードし、得られた画像である。

import numpy as np 
import matplotlib.pyplot as plt 
from numpy import linspace 
import cubehelix 

# Create random events and non-events 
x_noneve = 3.*np.random.randn(10000) +22. 
np.random.seed(seed=41) 

y_noneve = np.random.randn(10000) 
np.random.seed(seed=45) 

x_eve = 3.*np.random.randn(1000) +22. 
np.random.seed(seed=33) 

y_eve = np.random.randn(1000) 

x_all = np.concatenate((x_eve,x_noneve),axis=0) 
y_all = np.concatenate((y_eve,y_noneve),axis=0) 

# Set up default x and y limits 
xlims = [min(x_all),max(x_all)] 
ylims = [min(y_all),max(y_all)] 

# Set up your x and y labels 
xlabel = 'Falling Star' 
ylabel = 'Fairy Godmother' 

# Define the locations for the axes 
left, width = 0.12, 0.55 
bottom, height = 0.12, 0.55 
bottom_h = left_h = left+width+0.03 

# Set up the geometry of the three plots 
rect_wishes = [left, bottom, width, height] # dimensions of wish plot 
rect_histx = [left, bottom_h, width, 0.25] # dimensions of x-histogram 
rect_histy = [left_h, bottom, 0.25, height] # dimensions of y-histogram 

# Set up the size of the figure 
fig = plt.figure(1, figsize=(9.5,9)) 
fig.suptitle('Wishes coming true', fontsize=18, fontweight='bold') 

cx1 = cubehelix.cmap(startHue=240,endHue=-300,minSat=1,maxSat=2.5,minLight=.3,maxLight=.8,gamma=.9) 

# Make the three plots 
axWishes = plt.axes(rect_wishes) # wishes plot 
axStarx = plt.axes(rect_histx) # x bar chart 
axFairy = plt.axes(rect_histy) # y bar chart 

# Define the number of bins 
nxbins = 50 
nybins = 50 
nbins = 100 

xbins = linspace(start = xlims[0], stop = xlims[1], num = nxbins) 
ybins = linspace(start = ylims[0], stop = ylims[1], num = nybins) 
xcenter = (xbins[0:-1]+xbins[1:])/2.0 
ycenter = (ybins[0:-1]+ybins[1:])/2.0 

delx = np.around(xbins[1]-xbins[0], decimals=2,out=None) 
dely = np.around(ybins[1]-ybins[0], decimals=2,out=None) 

H, xedges,yedges = np.histogram2d(y_eve,x_eve,bins=(ybins,xbins)) 
X = xcenter 
Y = ycenter 
H = np.where(H==0,np.nan,H) # Remove 0's from plot 

# Plot the 2D histogram 
cax = (axWishes.imshow(H, extent=[xlims[0],xlims[1],ylims[0],ylims[1]], 
     interpolation='nearest', origin='lower',aspect="auto",cmap=cx1)) 

#Plot the axes labels 
axWishes.set_xlabel(xlabel,fontsize=14) 
axWishes.set_ylabel(ylabel,fontsize=14) 

#Set up the plot limits 
axWishes.set_xlim(xlims) 
axWishes.set_ylim(ylims) 

#Set up the probability bins 
x_eve_hist, xoutbins = np.histogram(x_eve, bins=xbins) 
y_eve_hist, youtbins = np.histogram(y_eve, bins=ybins) 

x_noneve_hist, xoutbins = np.histogram(x_noneve, bins=xbins) 
y_noneve_hist, youtbins = np.histogram(y_noneve, bins=ybins) 

probax = [eve/(eve+noneve+0.0) if eve+noneve>0 else 0 for eve,noneve in zip(x_eve_hist,x_noneve_hist)] 
probay = [eve/(eve+noneve+0.0) if eve+noneve>0 else 0 for eve,noneve in zip(y_eve_hist,y_noneve_hist)] 

probax = probax/np.sum(probax) 
probay = probay/np.sum(probay) 

probax = np.round(probax*100., decimals=0, out=None) 
probay = np.round(probay*100., decimals=0, out=None) 

#Plot the bar charts 

#Set up the limits 
axStarx.set_xlim(xlims[0], xlims[1]) 
axFairy.set_ylim(ylims[0], ylims[1]) 

axStarx.bar(xcenter, probax, align='center', width =delx, color = 'royalblue') 
axFairy.barh(ycenter,probay,align='center', height=dely, color = 'mediumorchid') 

#Show the plot 
plt.show() 

resulting image

hex version

+0

こんにちは、ようこそ。あなたのコードに*問題はありますか?それはうまくいくように思われます。あなたは、より良いものとそうでないものについて、人々の*意見*を求めています。これは必ずしもSOに関するものではありません。非常に素敵なプロット、)ところで –

+0

ありがとう:)と私は新しいことがわかりますように、SOの悪用のおかげで申し訳ありません。 このコードで主な問題は、(i)棒グラフのビンを正しく整列させることでした.-私は、xy軸の限界を設定して中心を使用すると、棒グラフと(ii)imshowの使用。このコードのはるかに複雑なバージョンでは、私は原点を「上」と「下」としてみましたが、それは常にバーカルトと比べて逆さまになりました。残念ながら、私はより複雑なバージョンを投稿できません。 – SpicyBaguette

+0

あなたはこれを見たことがありますか:http://matplotlib.org/examples/pylab_examples/scatter_hist.html? –

答えて

0

私の元のコードが機能的であったが、2次元ヒストおよび棒グラフの制限が定義されていませんでしたヒストグラムビンを使用します。したがって、ビンに変更を加えると、あまり整列していないグラフが作成されました。グラフの制限が常にヒストグラムビンの限界に対応するように、私は

axStarx.set_xlim(axWishes.get_xlim()) 
axFairy.set_ylim(axWishes.get_ylim()) 

cax = (axWishes.imshow(H, extent=[xbins[0],xbins[-1],ybins[0],ybins[-1]], 
     interpolation='nearest', origin='lower',aspect="auto",cmap=cx1)) 

axStarx.set_xlim(xlims[0], xlims[1]) 
axFairy.set_ylim(ylims[0], ylims[1]) 

cax = (axWishes.imshow(H, extent=[xmin,xmax,ymin,ymax], 
     interpolation='nearest', origin='lower',aspect="auto",cmap=cx1)) 

を変更しました

詳細については、棒グラフはeを受け入れることができます棒の位置として軸に沿ってインデックスまたは値を繰り返します。バーがカテゴリ変数ではなくビンに対応する場合、軸の限界を設定し、バーの幅を正しく定義することが重要です。これらはhistoで自動的に行われます。ただし、ビンでメンバーの数以外の変数を探索する場合は、棒グラフを使用して限界値を手動で定義する必要があります。

関連する問題