2017-02-17 125 views
-1

是否有一種簡單的方法用圖像兩側的x軸和y軸繪製像素強度平均值的2D數據?類似於seaborn的jointplot(doc),但使用2D numpy陣列作爲輸入?或者,也許numpy數組可以很容易地轉換成可以散點圖的形式?沿着python中的圖像陣列軸的平均投影

醜陋的解決方法是將圖像轉換爲x和y座標。然後,我可以使用jointplot,但圖像輸出非常難看。

img=#some 2d image data 
xx=np.zeros(img.sum()) 
yy=np.zeros(img.sum()) 
i=0 
for x in range(img.shape[0]): 
    for y in range(img.shape[1]): 
     for c in range(img[x,y]): 
      xx[i]=x 
      yy[i]=y 
      i+=1 

import seaborn as sns    
sns.jointplot(yy,xx) 

enter image description here

+0

目前還不清楚你的期望沿x或y軸的像素強度直方圖。你能詳細說明一下嗎? – kazemakase

+0

我在這裏找到了一個相當不錯的解決方案:http://stackoverflow.com/questions/20525983/matplotlib-imshow-a-2d-array-with-plots-of-its-marginal-densities – jlarsch

+0

不像seaborn那麼光滑,但一個好的開始 – jlarsch

回答

0

通過在我的評論中link建議的啓發,我想出了下面的美化版本:

from matplotlib import gridspec 
img=tmp 
fig=plt.figure(figsize=(6, 6)) 
t = np.arange(img.shape[0]) 
f = np.arange(img.shape[1]) 
flim = (f.min(), f.max()) 
tlim = (t.min(), t.max()) 


gs = gridspec.GridSpec(2, 2, width_ratios=[5,1], height_ratios=[1,5]) 
gs.update(hspace=0, wspace=0) 
ax = fig.add_subplot(gs[1,0]) 
axl = fig.add_subplot(gs[1,1], sharey=ax) 
axb = fig.add_subplot(gs[0,0], sharex=ax) 
plt.setp(axl.get_yticklabels(), visible=False) 
plt.setp(axb.get_xticklabels(), visible=False) 
plt.setp(axl.get_xticklabels(), visible=False) 
plt.setp(axb.get_yticklabels(), visible=False) 

ax.imshow(img, origin='lower',aspect='equal') 

axl.fill_between(img.mean(1), f) 
axb.fill_between(t, img.mean(0)) 
ax.set_xlim(tlim) 
ax.set_ylim(tlim) 

enter image description here