【发布时间】:2014-03-26 17:08:03
【问题描述】:
据我所知,matplotlib 箱线图函数中胡须的末端延伸到最大值低于 75% + 1.5 IQR 和最小值高于 25% - 1.5 IQR。我想将其更改为表示数据的最大值和最小值或数据的第 5 和第 95 个四分位数。可以这样做吗?
【问题讨论】:
标签: python matplotlib
据我所知,matplotlib 箱线图函数中胡须的末端延伸到最大值低于 75% + 1.5 IQR 和最小值高于 25% - 1.5 IQR。我想将其更改为表示数据的最大值和最小值或数据的第 5 和第 95 个四分位数。可以这样做吗?
【问题讨论】:
标签: python matplotlib
要让胡须出现在数据的最小值和最大值处,请将whis 参数设置为任意大的数字。换句话说:boxplots = ax.boxplot(myData, whis=np.inf)。
whis kwarg 是四分位距的比例因子。胡须被绘制到whis * IQR 内远离四分位数的最外层数据点。
在 matplotlib v1.4 中,您可以说:boxplots = ax.boxplot(myData, whis=[5, 95]) 将胡须设置在第 5 和第 95 个百分位数。同样,您可以说 boxplots = ax.boxplot(myData, whis='range') 将胡须设置为最小值和最大值。
注意:您可能可以修改 ax.boxplot 方法返回的 boxplots 字典中包含的艺术家,但这似乎很麻烦
【讨论】:
whis=np.inf,给了我一堆RuntimeWarning: invalid value encountered in double_scalars 等; whis='range' 工作正常
whis='range 现在是实现以前使用 whis=np.inf 解决方法的正确方法
设置箱线图选项whisk=0 来隐藏内置的胡须。然后创建自定义晶须,显示 5% 到 95% 的数据。
#create markings that represent the ends of whiskers
low=data.quantile(0.05)
high=data.quantile(0.95)
plt.scatter(range(1,len(low)+1),low,marker='_')
plt.scatter(range(1,len(low)+1),high,marker='_')
#connects low and high markers with a line
plt.vlines(range(1,len(low)+1),low,high)
这应该会在 5% 到 95% 的盒子后面创建带有胡须标记的垂直线。
【讨论】: