pyplot.boxplot 如何让盒子变薄

How to make box thinner in pyplot.boxplot

当我使用箱线图时,仅针对一组数据(即一个数组),我的方框实际上被拉伸到 window 的整个宽度: screenshot of result

我尝试在 plt.boxplot 中使用 "widths" 参数,但它没有改变任何东西:

code's screenshot

我想让盒子变薄(比方说 window 的 30% 左右)。我做错了什么?

您可以使用:

plt.tight_layout(pad=10)
#10 spaces in font size units between figure and window border
plt.show()

x 轴自动缩放到框的扩展范围。这个问题似乎只出现在 matplotlib 2.0 之前的版本中。

避免这种情况的一种简单方法是在 x 轴上设置一些限制,例如

plt.xlim(0.5, 1.5)

然后可以尝试限制,直到它看起来最吸引人。