从matplotlib找到异常点:boxplot

时间:2012-04-19 23:36:45

标签: python matplotlib outliers

我正在使用boxplot绘制非正态分布,并有兴趣使用matplotlib的boxplot函数找出异常值。

除了情节之外,我有兴趣找到我的代码中的点的值,这些点在箱图中显示为异常值。有什么方法可以从boxplot对象中提取这些值以用于我的下游代码吗?

1 个答案:

答案 0 :(得分:17)

你的意思是指两条黑线上方和下方的那些点吗?

from pylab import *
spread= rand(50) * 100
center = ones(25) * 50
flier_high = rand(10) * 100 + 100
flier_low = rand(10) * -100
data =concatenate((spread, center, flier_high, flier_low), 0)
r = boxplot(data)

enter image description here

存储来自boxplot的返回字典,您可以从中获取所有信息,例如:

top_points = r["fliers"][0].get_data()[1]
bottom_points = r["fliers"][2].get_data()[1]
plot(np.ones(len(top_points)), top_points, "+")
plot(np.ones(len(bottom_points)), bottom_points, "+")

enter image description here