Label Q1,Q2,Q3,min and max on horizontal box and whisker plot on python

Viewed 727

I want to label a horizontal box and whisker plot by labeling its Q1,Q2,Q3, min and max. There are many answers on this site but every answer is focused on a vertical box and whisker plot. These are my codes as follows:

import matplotlib.pyplot as plt;
import seaborn as sns;sns.set()
from numpy import *;

fig, (ax1,ax2) =plt.subplots(2)
a = 1000+random.randn(1000)
print(a)
red_square = dict(markerfacecolor='r', marker='s')
ax1.boxplot(a, vert=False, flierprops=red_square)
plt.show()

Can you help me please? enter image description here

1 Answers

You can use boxplot_stats from matplotlib to get the Q1,Q2 and median (or you calculate yourself basically). The min and the max, is not included and you have to calculate that. Something roughly like below should work:

import matplotlib.pyplot as plt;
import seaborn as sns;sns.set()
from numpy import *;

fig,ax1 =plt.subplots(1)
a = 1000+random.randn(1000)

red_square = dict(markerfacecolor='r', marker='s')
ax1.boxplot(a, vert=False, flierprops=red_square)

from matplotlib import cbook
data = cbook.boxplot_stats(a)

values = [data[0][i] for i in ['q1','med','q3']] + [min(a),max(a)]
for v in values:
    ax1.text(v,1.2,str(round(v,1)),fontsize=8,horizontalalignment='center')

enter image description here

Related