2011-07-04 50 views
4

我想在matplotlib中創建一個2d直方圖的2x3圖,在每個子圖頂部有一個共享的顏色條和一個直方圖。 AxesGrid除了最後一部分以外,還給了我一切。我試圖通過使用make_axes_locatable在上面的頁面上按照"scatter_hist.py" example在每個子圖的頂部添加2d直方圖。該代碼看起來是這樣的:將共享軸圖添加到matplotlib中的AxesGrid圖

plots = [] 
hists = [] 
for i, s in enumerate(sim): 
    x = np.log10(s.g['temp']) #just accessing my data 
    y = s.g['vr'] 
    histy = s.g['mdot'] 
    rmin, rmax = min(s.g['r']), max(s.g['r']) 
    plots.append(grid[i].hexbin(x, y, C = s.g['mass'], 
       reduce_C_function=np.sum, gridsize=(50, 50), 
       extent=(xmin, xmax, ymin, ymax), 
       bins='log', vmin=cbmin, vmax=cbmax)) 
    grid[i].text(0.95 * xmax, 0.95 * ymax, 
       '%2d-%2d kpc' % (round(rmin), round(rmax)), 
       verticalalignment='top', 
       horizontalalignment='right') 

    divider = make_axes_locatable(grid[i]) 
    hists.append(divider.append_axes("top", 1.2, pad=0.1, sharex=plots[i])) 
    plt.setp(hists[i].get_xticklabels(), visible=False) 
    hists[i].set_xlim(xmin, xmax) 
    hists[i].hist(x, bins=50, weights=histy, log=True) 

#add color bar 
cb = grid.cbar_axes[0].colorbar(plots[i]) 
cb.set_label_text(r'Mass ($M_{\odot}$)') 

這在divider.append_axes給出了一個錯誤()函數調用:

AttributeError: 'LocatablePolyCollection' object has no attribute '_adjustable' 

有誰知道它是否能夠容易地將直方圖添加到與頂部axisgrid方法,還是我需要使用不同的方法?謝謝!

+0

你還在努力嗎?你找到了解決方案嗎? – Yann

回答

1

您應該在divider.append_axes的調用中向sharex關鍵字提供AxesSubplot(它具有_adjustable屬性)的實例。取而代之的是,您將返回值hexbin添加到此關鍵字參數中,該參數是LocatablePolyCollection的一個實例。

因此,如果您在divider.append_axes的呼叫中將sharex=plots[i]替換爲sharex=grid[i],那麼您的代碼應該可以正常工作。

相關問題