我正在使用matplotlib创建一个boxplot。我怎样才能得到方框内的积分?
import matplotlib.pyplot as plt
data = [2,4,5,62,57,3,8,3,96,2,34,43]
box=plt.boxplot(data)
答案 0 :(得分:0)
框内的点是四分位数范围内的值。可以使用np.quartile
计算第1和第3个四分位数,一旦您知道这些值,就可以有条件地对数据数组进行采样:
import matplotlib.pyplot as plt
import numpy as np
data = np.array([2,4,5,62,57,3,8,3,96,2,34,43])
box=plt.boxplot(data)
# Get all the data between the 1st and 3rd quartile
data_iqr = data[ (data > np.percentile(data, 25)) & (data < np.percentile(data, 75)) ]
# Visual confirmation:
plt.scatter(np.ones_like(data_iqr), data_iqr, marker='o', label='IQR')
plt.scatter(np.ones_like(data), data, marker='x', label='all')
plt.legend(frameon=False, loc='best')