Overlaying a box plot or mean with error bars on a histogram

4.3k Views Asked by At

I am creating a histogram in Seaborn of my data in a pretty standard way, ie:

rc = {'font.size': 32, 'axes.labelsize': 28.5, 'legend.fontsize': 32.0, 
    'axes.titlesize': 32, 'xtick.labelsize': 31, 'ytick.labelsize': 12}
sns.set(style="ticks", color_codes=True, rc = rc)
plt.figure(figsize=(25,20),dpi=300)

ax = sns.distplot(synData['SYNERGY_SCORE'])
print (np.mean(synData['SYNERGY_SCORE']), np.std(synData['SYNERGY_SCORE']))
# ax = sns.boxplot(synData['SYNERGY_SCORE'], orient = 'h') 

ax.set(xlabel = 'Synergy Score', ylabel = 'Frequency', title = 'Aggregate Synergy Score Distribution')

This produces the following output: standard histogram.

I also want to visualize the mean + standard deviation of this dataset on the same plot, ideally by having a point for the mean on the x-axis (or right above the x-axis) and notched error bars showing the standard deviation. Another option is a boxplot hugging the x-axis. I tried just adding the line which is commented out (sns.boxplot()), but it looks super ugly and not at all what I'm looking for. Any suggestions?

1

There are 1 best solutions below

0
On

The boxplot is drawn on a categorical axis and won't coexist nicely with the density axis of the histogram, but it's possible to do it with a twin x axis plot:

import numpy as np
import seaborn as sns
np.random.seed(2023)  # ensures reproducible data

x = np.random.randn(300)
ax = sns.distplot(x)
ax2 = ax.twinx()
sns.boxplot(x=x, ax=ax2)
ax2.set(ylim=(-.5, 10))

enter image description here


sns.distplot is deprecated in favor of sns.histplot

ax = sns.histplot(x, kde=True, stat='density')
ax2 = ax.twinx()
sns.boxplot(x=x, ax=ax2)
ax2.set(ylim=(-.5, 10))

enter image description here