2016-07-14 44 views
1

我有我已經從彙總表產生的,而不是原始數據factorplot添加簡單的錯誤酒吧Seaborn factorplot

http://dsh.re/db165

使用下面的代碼:

sns.factorplot(col="followup", y="probability", hue="next intervention", x="age", 
       data=table_flat[table_flat['next intervention']!='none'], 
       facet_kws={'ylim':(0,0.6)}) 

的標繪這裏是彙總表的平均值,但我也想繪製可信區間,其上限和下限在其他兩列中指定。該表是這樣的:

http://dsh.re/0bf74

有沒有一種方法,也許使用附上錯誤酒吧點的factorplot返回FacetGrid

+0

當然,使用'plt.errorbar' – mwaskom

+0

我試過以下內容:'g.map_dataframe(plt.errorbar,x =「followup(months)」,y =「probability」,yerr ='sd')'其中g是'FacetGrid',但沒有任何東西添加到情節。 –

+1

使用參數,而不是kwargs – mwaskom

回答

1

你可以通過plt.errorbarFacetGrid.map但它需要一個小包裝的功能才能正常格式化參數(和明確地傳遞的類別順序):

import numpy as np 
from scipy import stats 
import seaborn as sns 
import matplotlib.pyplot as plt 

# Reformat the tips dataset to your style 
tips = sns.load_dataset("tips") 
tips_agg = (tips.groupby(["day", "smoker"]) 
       .total_bill.agg([np.mean, stats.sem]) 
       .reset_index()) 
tips_agg["low"] = tips_agg["mean"] - tips_agg["sem"] 
tips_agg["high"] = tips_agg["mean"] + tips_agg["sem"] 

# Define a wrapper function for plt.errorbar 
def errorbar(x, y, low, high, order, color, **kws): 
    xnum = [order.index(x_i) for x_i in x] 
    plt.errorbar(xnum, y, (y - low, high - y), color=color) 

# Draw the plot 
g = sns.factorplot(x="day", y="mean", col="smoker", data=tips_agg) 
order = sns.utils.categorical_order(tips_agg["day"]) 
g.map(errorbar, "day", "mean", "low", "high", order=order) 

enter image description here

+0

這是有益的,謝謝! –