How to show the value of quantiles in a boxplot
Question:
I managed to get a boxplot of 2 categories in the x-axis and a continuous variable in the y-axis. I just want to add to the plot the value of the quartiles, near to the boxes.
Like this:
Answers:
Here is an example:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
np.random.seed(0)
df = pd.DataFrame({'churn': np.random.choice(['No', 'Si'], size=1000),
'value': np.random.random(size=1000)})
box_width = 0.5
ax = sns.boxplot(data=df, x='churn', y='value', width=box_width)
i = 0
for name, group in df.groupby('churn'):
Q1, Q3 = group['value'].quantile([0.25,0.75])
for q in (Q1, Q3):
x = i-box_width/2
y = q
ax.annotate('%.2f' % q, (x,y),
xytext=(x-0.1, y), textcoords='data',
arrowprops=dict(facecolor='black', shrink=0.05),
va='center', ha='right')
i+=1
I managed to get a boxplot of 2 categories in the x-axis and a continuous variable in the y-axis. I just want to add to the plot the value of the quartiles, near to the boxes.
Like this:
Here is an example:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
np.random.seed(0)
df = pd.DataFrame({'churn': np.random.choice(['No', 'Si'], size=1000),
'value': np.random.random(size=1000)})
box_width = 0.5
ax = sns.boxplot(data=df, x='churn', y='value', width=box_width)
i = 0
for name, group in df.groupby('churn'):
Q1, Q3 = group['value'].quantile([0.25,0.75])
for q in (Q1, Q3):
x = i-box_width/2
y = q
ax.annotate('%.2f' % q, (x,y),
xytext=(x-0.1, y), textcoords='data',
arrowprops=dict(facecolor='black', shrink=0.05),
va='center', ha='right')
i+=1