距离数据的箱线图 - 手动设置箱体值

0 投票
1 回答
1039 浏览
提问于 2025-04-18 14:24

我有一堆二维的点和角度。为了展示这些点的移动情况,我想用箱线图来表示这些点与平均值之间的差异。

我成功地用Python和matplotlib可视化了角度的抖动,得到了下面这个箱线图:

enter image description here

现在我想对我的位置数据做同样的事情。在计算了欧几里得距离后,所有的数据都是正数,所以简单的箱线图会给出错误的结果。比如,下面这个箱线图中,正好在平均值上的点距离为零,但现在却被视为异常值。

所以我想问:

我该如何手动把箱线图的下边界和须的部分设置为零?如果我应该换个方法,比如用条形图,请告诉我(不过我还是想用同样的风格)。

编辑:目前的图看起来像下面这个(这是角度与它们平均值之间距离的图)。

如你所见,箱线图没有覆盖到零。这对于数据来说是正确的,但对于它背后的意义就不对了!零是完美的(因为它代表了一个正好在角度中间的点),但在箱线图中并没有包含。

enter image description here

1 个回答

0

我发现这个问题之前在StackOverflow上已经有人问过了。虽然不是完全一样的问题,但那个问题里有答案!

在matplotlib 1.4版本中,可能会有更快的方法来解决这个问题,不过目前来看,另一个讨论里的答案似乎是最好的选择。

编辑: 结果我发现我不能用他们的方法,因为我需要使用plt.boxplot(data, patch_artist=True)来实现其他一些好看的效果。

所以我不得不使用以下这个不太优雅的最终解决方案:

N = 12 #number of my plots

upperBoxPoints= []
for d in data:
    upperBoxPoints.append(np.percentile(d, 75)) 

w = 0.5 # i had to tune the width by hand
ind = range(0,N)  #compute the correct placement from number and width
ind = [x + 0.5+(w/2) for x in ind] 


for i in range(N):
    rect = ax.bar(ind[i], menMeans[i], w,  color=color[i], edgecolor='gray', linewidth=2, zorder=10)
# ind[i] position
# menMeans[i] hight of box
# w width
# color=color[i] as you can see i have a complex color scheme, use '#AAAAAAA' for colors, html names won't work
# edgecolor='gray' just like the other one
# linewidth=2 dito
# zorder=2 IMPORTANT you have to use at least 2 to draw it over the other stuff (but not to high or it is over your horizontal orientation lines

最后的结果是:

enter image description here

撰写回答