Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

python - Seaborn factor plot custom error bars

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

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)

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.

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

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

Is there a way to do this using seaborn factorplot or FacetGrid combined with matplotlib?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...