2015-07-01 49 views
1

我正在編寫一個Seaborn facetgrid,其中包含一些使網格空白的數據。此外,我用一些統計數據來標註每個方面,但是我不確定如何「跳過」空白方面,以便註釋落在適當的方面。跳過seaborn facetgrid中的空面用於註釋

g.axes.flat長度9(9個方面有數據);然而,當我在g.axes.flat中的每個元素上放置註釋時,它不會被放置在我所期望的位置。

g = sns.FacetGrid(mapping, col=options.facetCol, row=options.facetRow, col_order=sorted(cols), hue=options.group, sharex=False) 
g = g.map(sns.distplot, options.axis) 

# label each facet with stats 
grouped = mapping.groupby([options.facetRow, options.facetCol]) 
for ax, (name, df) in zip(g.axes.flat, grouped): 
    df2 = df.groupby(options.group) # group by each thing that has its own color and run stats on it 

    for i, (group, data) in enumerate(df2): 
     x = data[options.axis] 

     # calculate stats and create label 
     n = len(x) 
     mean = np.mean(x) 
     std = np.std(x) 
     label = r"%s: n=%s, $\mu$=%.2f $\sigma$=%.2f" %(group, n, mean, std) 
     ax.annotate(label, xy=(0.05,0.9-(i*0.05)), xycoords='axes fraction', ha='left', size=8) 

enter image description here

編輯

我創建了一個註釋功能,我把它傳遞給map() [推薦]但我不確定如何將標籤名稱傳遞給函數,以及如何獲得註釋(每個方面有兩個)在y方向上移動。還有什麼建議?

g = g.map(stats, options.axis) 

def stats(x, **kwargs): 
    ax = sns.distplot(x, **kwargs) 

    # calculate stats and create label 
    n = len(x) 
    mean = np.mean(x) 
    std = np.std(x) 
    label = r"%s: n=%s, $\mu$=%.2f $\sigma$=%.2f" %('moo', n, mean, std) # temporary label, need to pass it through function 
    i = 1 # temporary, needs to increment to shift annotations so they aren't on top of each other 

    # create annotation 
    ax.annotate(label, xy=(0.05,0.9-(i*0.05)), xycoords='axes fraction', ha='left', size=8) 

enter image description here

+1

這比seaborn一個以上的熊貓問題;您需要遍歷所有組,而不僅僅是那些有數據的組。但最好的做法是定義一個執行註釋的函數,然後將其傳遞給'g.map'。 – mwaskom

+1

要移動註釋,有點破解,但我會做'y = .7如果ax.texts else .8'。我不確定「將標籤名稱傳遞給函數」是什麼意思,但是'map'會將色調級別的名稱傳遞給'label'參數。 – mwaskom

回答

2

最終的溶液爲:

g = sns.FacetGrid(mapping, col=options.facetCol, row=options.facetRow, col_order=sorted(cols), hue=options.group, sharex=False) 
g.map(sns.distplot, options.axis) 
g.map(stats, options.axis) 

# custom function that allows us to create a distplot and add offset annotations to each facet that is not empty 
def stats(x, label, **kwargs): 

    # get a reference to the currently active axes 
    ax = plt.gca()  

    # calculate stats and create label 
    n = len(x) 
    mean = np.mean(x) 
    std = np.std(x) 
    label = r"%s: n=%s, $\mu$=%.2f $\sigma$=%.2f" %(label, n, mean, std) 

    # create annotation 
    y = 0.9 - len(ax.texts) * 0.05 
    ax.annotate(label, xy=(0.05,y), xycoords='axes fraction', ha='left', size=8) 
+0

我編輯了代碼,將'distplot'調用從'stats'中直接傳遞給'map',這使得註釋代碼更加通用。 – mwaskom

+0

'ax'來自哪裏?我想它一定是一個Axes對象,但你怎麼得到它?謝謝! – nickos556

+0

因爲我不確定,我會讓@mwaskom鐘聲;在此期間,原始答案在這裏找到:https://stackoverflow.com/revisions/382818e2-a6e4-4136-9eda-dbae5195ee10/view-source – Constantino

相關問題