Seaborn因子图自定义误差线 [英] Seaborn factor plot custom error bars
问题描述
我想在seaborn中绘制一个因子图,但是手动提供误差线,而不是由seaborn计算它们.
I'd like to plot a factorplot in seaborn but manually provide the error bars instead of having seaborn calculate them.
我有一个熊猫数据框,看起来像这样:
I have a pandas dataframe that looks roughly like this:
model output feature mean std
0 first two a 9.00 2.00
1 first one b 0.00 0.00
2 first one c 0.00 0.00
3 first two d 0.60 0.05
...
77 third four a 0.30 0.02
78 third four b 0.30 0.02
79 third four c 0.10 0.01
并且我正在输出一个大致如下所示的图:
and I'm outputting a plot that looks roughly like this:
我正在使用以下seaborn命令生成图:
I'm using this seaborn commands to generate the plot:
g = sns.factorplot(data=pltdf, x='feature', y='mean', kind='bar',
col='output', col_wrap=2, sharey=False, hue='model')
g.set_xticklabels(rotation=90)
但是,我无法弄清楚如何使用'std'列作为误差线.不幸的是,重新计算相关数据帧的输出将非常耗时.
However, I can't figure out how to have seaborn use the 'std' column as the error bars. Unfortunately, it would be quite time consuming to recompute the output for the data frame in question.
这与以下q有点类似: 使用Seaborn FacetGrid从数据框中绘制误差线
This is a little similar to this q: Plotting errors bars from dataframe using Seaborn FacetGrid
除了我不知道如何使它与matplotlib.pyplot.bar函数一起使用.
Except I can't figure out how to get it to work with the matplotlib.pyplot.bar function.
是否有一种方法可以将seaborn的factorplot
或FacetGrid
与matplotlib结合使用?
Is there a way to do this using seaborn factorplot
or FacetGrid
combined with matplotlib?
谢谢!
推荐答案
您可以做类似的事情
import seaborn as sns
import matplotlib.pyplot as plt
from scipy.stats import sem
tips = sns.load_dataset("tips")
tip_sumstats = (tips.groupby(["day", "sex", "smoker"])
.total_bill
.agg(["mean", sem])
.reset_index())
def errplot(x, y, yerr, **kwargs):
ax = plt.gca()
data = kwargs.pop("data")
data.plot(x=x, y=y, yerr=yerr, kind="bar", ax=ax, **kwargs)
g = sns.FacetGrid(tip_sumstats, col="sex", row="smoker")
g.map_dataframe(errplot, "day", "mean", "sem")
这篇关于Seaborn因子图自定义误差线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!