0

I'm trying to add the labels for the quartiles and median for Seaborn's boxplots, any ideas?

import seaborn as sns

sns.set_style("whitegrid")
tips = sns.load_dataset("tips")
plt.figure(figsize=(20,8))
box_plot = sns.boxplot(x="total_bill",y="day", orient='h', data=tips)

For example:

enter image description here

mikelowry
  • 1,307
  • 4
  • 21
  • 43
  • 1
    Yes, it shows the medians in the duplicate, but it also shows how that is done. So you have to calculate the values for each component (e.g. `medians = tips.groupby(['day'])['total_bill'].median()`) and add them as text annotations. – Trenton McKinney Jun 09 '22 at 19:34
  • See the new [answer](https://stackoverflow.com/a/72566882/7758804) at the duplicate to correctly locate, and annotate all of the positions (including whiskers) – Trenton McKinney Jun 10 '22 at 16:28

1 Answers1

2

Well, if you're still interested I made a quick example model:

Given this generic example boxplot:

import matplotlib.pyplot as plt
import seaborn as sns

sns.set_style("whitegrid")
tips = sns.load_dataset("tips")
plt.figure(figsize=(20,8))
box = sns.boxplot(x="total_bill",y="day", orient='h', data=tips)

From which I look for min, median and max values:

df1 = tips.groupby('day')['total_bill'].agg(Min='min', Median='median', Max='max')
df1

Do the same for quartiles and/or percentiles

day Min Median Max
Thur 7.51 16.2 43.11
Fri 5.75 15.38 40.17
Sat 3.07 18.24 50.81
Sun 7.25 19.63 48.17

Then iterating over new values to make annotations:

for i in range(len(df1.Min)):
  box.annotate(str(df1.Min[i]), xy=(df1.Min[i]-0.1,i), ha='right')

for i in range(len(df1.Median)):
  box.annotate(str(df1.Median[i]), xy=(df1.Median[i]-0.1,i), ha='right')

for i in range(len(df1.Max)):
  box.annotate(str(df1.Max[i]), xy=(df1.Max[i]+0.1,i), ha='left')

enter image description here

Note:

From there you can fully customize size, color, font, vertical/horizontal alignement and so on... :)

Drakax
  • 1,305
  • 3
  • 9