0

イベントの発生に対する 2 つの変数の影響を視覚化するために、matplotlib を使用して 2D ヒストグラムをプロットしたいと思います。

私のテストケースでは、イベントは「願いが叶う」であり、変数xは流れ星の数でyあり、妖精の名付け親の関与です。私がやりたいことは、流れ星と妖精のゴッドマザーのビンに叶う願いの2次元ヒストグラムをプロットすることです. 次に、各軸の横に、流れ星と妖精のゴッドマザーのビンごとに、願いが叶う確率、イベント/(イベント+非イベント) を示したいと思います (各ヒストグラム ビンの確率を含む 1D 棒グラフ)。棒グラフ ビンは、2 次元ヒストグラム ビンに対応し、整列する必要があります。ただし、棒グラフとヒストグラム ビンの間にはわずかなずれがあるようです。

棒グラフを正しく整列させるために、最初と最後のビンの端に対応する軸の制限の設定はうまくいきますか? 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()

結果の画像

16進バージョン

4

1 に答える 1

0

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

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

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])

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

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

于 2016-04-08T13:39:15.110 に答える