I would like to add median values to a boxplot in Seaborn where there are two levels of categorical data on the x-axis. How would I do this?
I saw this example and applied it to my problem, but due to the positioning of labels reliant on ax.get_xticklabels
, the median values of the first four values are applied in the middle of the outer categorical variable.
tips = sns.load_dataset("tips")
medians = \
tips.groupby(['day', 'smoker']).agg({'total_bill':np.median}).values
ax = sns.boxplot(x="day", y="total_bill", hue="smoker", data=tips)
pos = range(len(medians))
for tick,label in zip(pos,ax.get_xticklabels()):
ax.text(pos[tick], medians[tick] + 0.5, medians[tick],\
horizontalalignment='center', size='small')