4

What is the best way to add a point representing the mean (or another measure of central tendency) and a measure of variability (e.g., standard deviation or confidence interval) to each histogram in a seaborn FacetGrid?

The result should look similar to the figure shown here, but with a mean/SD in each of the FacetGrid subplots. This is a related question for the non-FacetGrid case.

Community
  • 1
  • 1
Troas
  • 1,167
  • 2
  • 9
  • 9
  • Probably helpful: https://seaborn.github.io/tutorial/axis_grids.html#mapping-custom-functions-onto-the-grid – mwaskom Oct 17 '16 at 13:16

1 Answers1

4

Based on @mwaskom's comment, here is one possible solution (using boxplot, analogous for pointplot):

tips = sns.load_dataset("tips")

sns.set(font_scale=1.3)

def dist_boxplot(x, **kwargs):
    ax = sns.distplot(x, hist_kws=dict(alpha=0.2))
    ax2 = ax.twinx()
    sns.boxplot(x=x, ax=ax2)
    ax2.set(ylim=(-5, 5))

g = sns.FacetGrid(tips, col="sex")
g.map(dist_boxplot, "total_bill");

"distplot plus boxplot"

(Not sure why the 0.01 is shifted slightly rightwards...)

Troas
  • 1,167
  • 2
  • 9
  • 9