pandas Seaborn 因子图自定义误差线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30385975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:23:12  来源:igfitidea点击:

Seaborn factor plot custom error bars

pythonpandasmatplotlibplotseaborn

提问by crackedegg

I'd like to plot a factorplot in seaborn but manually provide the error bars instead of having seaborn calculate them.

我想在 seaborn 中绘制因子图,但手动提供误差线而不是让 seaborn 计算它们。

I have a pandas dataframe that looks roughly like this:

我有一个大致如下所示的 Pandas 数据框:

     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 bar plots

我正在输出一个大致如下所示的图: seaborn 条形图

I'm using this seaborn commands to generate the plot:

我正在使用这个 seaborn 命令来生成图:

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)

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.

但是,我不知道如何让 seaborn 使用“std”列作为误差线。不幸的是,重新计算相关数据帧的输出会非常耗时。

This is a little similar to this q: Plotting errors bars from dataframe using Seaborn FacetGrid

这有点类似于 q: Plotting errors bar from dataframe using Seaborn FacetGrid

Except I can't figure out how to get it to work with the matplotlib.pyplot.bar function.

除了我不知道如何让它与 matplotlib.pyplot.bar 函数一起工作。

Is there a way to do this using seaborn factorplotor FacetGridcombined with matplotlib?

有没有办法使用 seabornfactorplotFacetGrid结合 matplotlib来做到这一点?

Thanks!

谢谢!

采纳答案by mwaskom

You could do something like

你可以做类似的事情

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")

enter image description here

在此处输入图片说明