Changing what the ends of whiskers represent in matplotlib’s boxplot function
我了解 matplotlib 的箱线图函数中的晶须末端延伸到最大值低于 75% 1.5 IQR 和最小值高于 25% – 1.5 IQR。我想将其更改为表示数据的最大值和最小值或数据的第 5 和第 95 个四分位数。可以这样做吗?
要让胡须出现在数据的最小值和最大值处,请将 whis 参数设置为任意大的数字。换句话说:boxplots = ax.boxplot(myData, whis=np.inf).
whis kwarg 是四分位数范围的比例因子。胡须被绘制到远离四分位数的 whis * IQR 内的最外层数据点。
现在 v1.4 已经发布:
在 matplotlib v1.4 中,您可以说: boxplots = ax.boxplot(myData, whis=[5, 95]) 将胡须设置在第 5 和第 95 个百分位数。同样,您可以说 boxplots = ax.boxplot(myData, whis=’range’) 将胡须设置为最小值和最大值。
注意:您可能可以修改 ax.boxplot 方法返回的 boxplots 字典中包含的艺术家,但这似乎很麻烦
- 用 matplotlib 3.1.1 尝试了 whis=np.inf,给了我一堆 RuntimeWarning: invalid value encountered in double_scalars 等; whis=’range’ 工作正常
- @sdbbs 警告是无害的。 whis=’range 现在是实现以前使用 whis=np.inf 解决方法的正确方法
设置箱线图选项whisk=0 来隐藏内置的胡须。然后创建自定义晶须,显示 5% 到 95% 的数据。
1
2 3 4 5 6 7 |
#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% 的盒子后面创建带有胡须标记的垂直线。
来源:https://www.codenong.com/21997897/