在 python matplotlib 中调整箱形图的宽度

Adjust width of box in boxplot in python matplotlib

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

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

documentation 中有一个宽度选项:

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.

这是一个例子:

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()

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

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