在python matplotlib中调整boxplot中框的宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32443803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:35:24  来源:igfitidea点击:

Adjust width of box in boxplot in python matplotlib

pythonmatplotlibboxplot

提问by user308827

I would like to reduce the width of the boxes in the boxplot below. Here's my code, but it is not working:

我想减少下面箱线图中框的宽度。这是我的代码,但它不起作用:

bp = plt.boxplot(boxes, widths = 0.6, patch_artist = True)

enter image description here

在此处输入图片说明

采纳答案by Jiloc

From the documentationthere is a widths option:

文档中有一个宽度选项:

widths : array-like, default = 0.5

Either a scalar or a vector and sets the width of each box. The default is 0.5, or 0.15*(distance between extreme positions) if that is smaller.

宽度:类似数组,默认值 = 0.5

标量或向量并设置每个框的宽度。默认值为 0.5,如果较小,则为 0.15*(极端位置之间的距离)。

Here is an example:

下面是一个例子:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(937)
data = np.random.lognormal(size=(37, 4), mean=1.5, sigma=1.75)
labels = list('ABCD')
fs = 10  # fontsize

plt.boxplot(data, labels=labels, showfliers=False, widths=(1, 0.5, 1.2, 0.1))

plt.show()

Result

结果

回答by Dimitris Fasarakis Hilliard

Try working via the axesand see if it works:

尝试通过 工作axes,看看它是否有效:

fig = plt.figure()
ax = fig.add_subplot(111)
ax.boxplot(boxes, widths = 0.6, patch_artist = True)