I want to have error bars in my bar plots when more than 3 data points are available (Condition A) but omit error bars when there are less than 3 data points for that specific condition (Condition B).
I've only found options to show or hide error bars for all bars, not for specific conditions.
import pandas as pd
import seaborn as sns
import numpy as np
df = pd.DataFrame(np.random.randint(0,100,size=(15)), columns=["Value"])
df["Label"]="Condition A"
df.Label[13:]="Condition B"
sns.barplot(data=df, x="Label", y="Value", errorbar="sd")
Actual Outcome: Error bars on all bars:
Desired outcome: Error bars on condition A only:
You can use a custom errorbar function in sns.barplot
. It should return a [y1, y2] iterable with the position of the min/max error:
# defining a custom function to only compute
# the error if more than 3 values
def cust_error(s):
if len(s)<3:
return [None, None]
else:
avg = s.mean()
std = s.std()
return [avg-std, avg+std]
sns.barplot(data=df, x="Label", y="Value", errorbar=cust_error)
Another option could be to plot the error bars manually:
ax = sns.barplot(data=df, x="Label", y="Value", errorbar=None)
g = df.groupby('Label', sort=False)
error = g['Value'].std().where(g.size()>=3)
plt.errorbar(range(len(s)), g['Value'].mean(), error,
ls='', color='#5F5F5F', lw=3)
Output: