我正在編寫一個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)
編輯
我創建了一個註釋功能,我把它傳遞給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)
這比seaborn一個以上的熊貓問題;您需要遍歷所有組,而不僅僅是那些有數據的組。但最好的做法是定義一個執行註釋的函數,然後將其傳遞給'g.map'。 – mwaskom
要移動註釋,有點破解,但我會做'y = .7如果ax.texts else .8'。我不確定「將標籤名稱傳遞給函數」是什麼意思,但是'map'會將色調級別的名稱傳遞給'label'參數。 – mwaskom