Seaborn boxplot quartile calculation

2019-08-28 16:59发布

i am using seaborn version 0.7.1 for python. I am trying to create a boxplot for the below numpy array

arr = np.array([2, 4, 5, 5, 8, 8, 9])

from my understanding the Quartiles Q1 and Q3 should be 4 and 8 but from the boxplot generated the Q1 is approximately 4.5. What am i missing ?

i am using the follwing command to generate the chart

sns.boxplot(arr)

enter image description here

1条回答
Summer. ? 凉城
2楼-- · 2019-08-28 17:33

It would of course depend on the definition of a quartile.

Wikipedia mentions 3 methods to calculate the quartile,

  • method1: Take median of the lower part of the sample [2,4,5]. Result 4.
  • method2: Take median of the lower part of the sample (including its median) [2,4,5,5]. Result 4.5.
  • method3: The lower quartile is 75% of the second data value plus 25% of the third data value. Result: 4*0.75+5*0.25 = 4.25. (It's always the mean between method1 and 2.

You may also use numpy to calculate the quartiles

x = [2, 4, 5, 5, 8, 8, 9]
np.percentile(x, [25])

This returns 4.5

查看更多
登录 后发表回答