2016-07-14 46 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

这是有益的,谢谢! –