1

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')

enter image description here

KT12
  • 549
  • 11
  • 24
  • One way to do it would be to look for the positions of the vertical lines and align the labels with that. [this post](https://stackoverflow.com/a/48541304/2454357) is not terribly related, but should give you an idea how to get hold of these lines. I would imagine looping through `ax.lines` and look for the ones where the `x` values don't change. – Thomas Kühn Jan 25 '19 at 06:32
  • Looks like the more severe problem is that the values are not actually correct. – ImportanceOfBeingErnest Jan 25 '19 at 12:51

0 Answers0