user308827
user308827

Reputation: 21961

Adjust width of box in boxplot in python matplotlib

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

Upvotes: 13

Views: 72989

Answers (2)

Jiloc
Jiloc

Reputation: 3658

From the documentation there 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.

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

Upvotes: 24

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160417

Try working via the axes and see if it works:

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

Upvotes: 4

Related Questions