2014-04-10 55 views
4

hexbin和histogram2d有什麼區別?hexbin和histogram2d的不同行爲

f, (ax1,ax2) = plt.subplots(2) 
ax1.hexbin(vradsel[0], distsel[0],gridsize=20,extent=-200,200,4,20],cmap=plt.cm.binary) 
H, xedges, yedges =np.histogram2d(vradsel[0], distsel[0],bins=20,range=[[-200,200],[4,20]]) 
ax2.imshow(H, interpolation='nearest', cmap=plt.cm.binary, aspect='auto',extent=[xedges[0],xedges[-1],yedges[0],yedges[-1]]) 
plt.show() 

您可以看到histogram2d給出了-90度的旋轉。我知道數據應該像hexbin圖。

Difference hexbin/histogram2d

+0

什麼,如果你比較'ax1.hexbin()''到ax2.hist2d( )'? – askewchan

+0

是的,這是區別。這兩個是一樣的。我現在沒有imshow()繪製其他東西,然後我的預期。 – Mathias711

回答

3

的區別不被計算直方圖的方式之間,而是在路上你繪製的直方圖。從np.histogram開始,您的數組H從數組左上角的4, -200 bin開始,但將根據您的默認值origin進行繪製。您可以使用origin=lowerorigin=upper關鍵字在plt.imshow中控制此項。

但是origin只是反映了圖像,所以還必須記住,在圖像中,水平軸x至上,縱軸y次之,陣列的相反,所以還必須繪製前轉H

我的建議只是使用plt.hist2d()代替,它將正確調整範圍和方向,與plt.hexbin一樣。您仍然可以訪問的結果與numpy的版本:H, x, y, im = ax.hist2d(...)但它使劇情自動


a = np.random.rand(100)*400 - 200 
b = np.random.rand(100)*16 + 4 
a[:10] = -200 
b[:10] = 4 

f, ax = plt.subplots(3) 

ax[0].hexbin(a, b, gridsize=20, extent=[-200,200,4,20], cmap=plt.cm.binary) 

H, xedges, yedges = np.histogram2d(a, b, bins=20, range=[[-200,200],[4,20]]) 
ax[1].imshow(H.T, interpolation='nearest', cmap=plt.cm.binary, aspect='auto', 
       extent=[xedges[0],xedges[-1],yedges[0],yedges[-1]], origin='lower') 

# simplest and most reliable: 
ax[2].hist2d(a, b, bins=20, range=[[-200,200],[4,20]], cmap=plt.cm.binary) 

hists

+0

啊,是的,我現在看到了。我可以使用'hist2d()'訪問創建的數組嗎?因爲我想用它做一個更多的操作(在每個y-bin中找到平均值)。我認爲最好使用#2選項(所以用'origin ='lower''標誌)。但謝謝澄清! – Mathias711

+0

是的,它返回的信息,但它也返回matplotlib對象,所以使用'H,xedges,yedeges,im = plt.hist2d(...)'所以我仍然建議如果你使用'plt.hist2d'無論如何計劃繪製它。 – askewchan

+0

這就是爲什麼我第一次嘗試(沒有'im')時出現'ValueError:太多值解壓縮'的錯誤。 – Mathias711