Seaborn factor plot custom error bars instead of bootstrapping
Asked Answered
R

2

19

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 like: 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?

Rain answered 21/5, 2015 at 23:23 Comment(0)
A
13

Tested in python 3.8.12, pandas 1.3.4, matplotlib 3.4.3, seaborn 0.11.2

You could do something like

import seaborn as sns
import matplotlib.pyplot as plt

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

Assault answered 24/5, 2015 at 22:2 Comment(2)
p = sns.catplot(kind='bar', data=tips, col="sex", row="smoker", x='day', y='total_bill', ci=68, height=3) seems the same using catplotTorrefy
Does this also work without FacetGrid using the axis level instead? I am looking for a solution that works with fig, ax = plt.subplots(1, 1) sns.barplot(ax=ax, data=data)Limpet
T
0

Here is another approach:

import matplotlib.pyplot as plt
import numpy as np

plt.plot(np.asarray([[0, 0], [1, 1]]).T, np.asarray([[0.3, 0.4], [0.01 , 0.02]]).T)
plt.show()

The x values correspond the categorical values of the bar chart (0 is the first category and so on). The y values show the upper and lower limits of the error bars. Both arrays must be transposed for matplotlib to display them correctly. I just find it to be more readable this way.

Error bars

Telemeter answered 1/9, 2020 at 8:10 Comment(1)
given the graph above, I think the second np.asarray([[0.3, 0.4], [0.01 , 0.02]]) should instead be np.asarray([[0.3, 0.4], [0.1 , 0.2]])Berceuse

© 2022 - 2024 — McMap. All rights reserved.