Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 392
user308827 Avatar asked Sep 07 '15 17:09

user308827


People also ask

Does the width of a box plot matter?

The length of the box becomes its height. The width across the page signifies nothing. Much more can be read from a boxplot than might be surmised from the simplistic method of its construction, particularly when the boxplots of several samples are lined up alongside one another (Parallel Boxplots).

What does boxplot width mean?

The widths of the box plot indicate the size of the samples. The wider the box, the larger the sample. This is usually an option in statistical software programs, not all Box Plots have the widths proportional to the sample size.

Which of the following parameter is used to change the position of boxplots matplotlib?

conf_intervals : This parameter is also an array or sequence whose first dimension is compatible with x and whose second dimension is 2. positions : This parameter is used to sets the positions of the boxes.


1 Answers

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

like image 78
Jiloc Avatar answered Sep 22 '22 08:09

Jiloc