python:在箱线图中设置胡须边界时出错

python: error setting whisker bounds in boxplot

当我尝试在箱线图中调整胡须长度时遇到了一个令人费解的错误——我希望使用 kwarg whis = [5,95] 将胡须从第 5 个百分位值延伸到第 95 个百分位值。当我这样做时,出现以下错误(复制整个回溯):

> Traceback (most recent call last):
>    File "boxplot_snoho_bc.py", line 125, in <module>
>      main()
>    File "boxplot_snoho_bc.py", line 64, in main
>      bp = boxplot(data[0], positions = [1], widths = 0.6, whis = [5,95])   
>    File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2143, in boxplot
>      ret = ax.boxplot(x, notch, sym, vert, whis, positions, widths, patch_artist, bootstrap)
>    File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 5545, in
> boxplot
>      wisk_hi = np.compress( d <= hi_val , d )
>  ValueError: operands could not be broadcast together with shapes (10) (42)

我从其他线程和帮助文档中了解到这是正确的语法,但我是 python 的新手,可能遇到了错误...

为了清楚起见,这里是导致错误的命令:

bp = boxplot(data[0], positions = [1], widths = 0.6, whis = [5,95])

data 是一个 8x10 的 numpy 数组;我一次只绘制一个盒子。数组如下所示:

print data

[[ 40.66 33.53 30.56 30.83 11.8 60.91 11.91 10.74 23.97 11. ]

[ 16.19 85.69 206.25 27.16 29.22 41.69 16.44 10.41 477.75 23.95]

[ 1651. 192. 16.02 29.91 14.95 123.38 31.22 11.08 29.5 16.03]

[ 132.5 874.5 12.51 41.5 68.25 60.22 36.91 12.27 58.84 28.33]

[ 145.5 26.95 25.2 33.88 44.78 40.16 16.78 18.95 36.03 16.05]

[ 18.89 82.94 83.56 344. 118.94 49.62 23.23 25.89 2072. 90.56]

[ 221.38 22.69 35.28 25.08 25.16 41.84 16.77 14.16 12.77 15.27]

[ 32.03 1634. 34.59 140.12 61.22 32.53 24.55 20.06 30.97 27.14]]

问题已解决。尴尬的是,这是 matplotlib 的版本问题(至少需要 1.4.0 版本)。