27

I want to create a boxplot using the minimum, maximum and average output and standard deviation of a dataset. The examples I found, plot a numerical distribution, but in my case, this is not feasible.

Is there any way to do this in Python (Matplotlib)?

0

2 Answers 2

50

The comment1. by @Roland, is important: a box plot shows fundamentally different quantities, and if you make a similar plot using the quantities you have, it might confuse users. I might represent this information using stacked errorbar plots.

import matplotlib.pyplot as plt
import numpy as np

# construct some data like what you have:
np.random.seed(2023)
x = np.random.randn(100, 8)
mins = x.min(0)
maxes = x.max(0)
means = x.mean(0)
std = x.std(0)

# create stacked errorbars:
plt.errorbar(np.arange(8), means, std, fmt='ok', lw=3)
plt.errorbar(np.arange(8), means, [means - mins, maxes - means],
             fmt='.k', ecolor='gray', lw=1)

enter image description here

1. A boxplot depicts quantiles. You can't derive these from what you have. I'd advise against creating a derivate of a box plot with mean, min, max, and sd since it would just confuse people familiar with box plots. I'd depict the mean, min, max as points (possibly with different symbols or size) and the sd as error bars. - Roland

0
0

Given the information available (mean, standard deviation, min, max), errorbar is probably the only graph that can be plotted but if, say, you want to plot a box plot from aggregated data, matplotlib has bxp() method that can be used. Note that it is an Axes-level function (cannot be called as plt.bxp). It uses a list of dictionaries where each dictionary contains the data about a specific boxplot.

However, boxplots need median, first and third quartiles at the minimum, which cannot be inferred from mean, standard deviation etc. without further assumptions about the distribution of the dataset.

Suppose the dataset is normally distributed. Then we can estimate median, first and third quartiles using mean and standard deviation. Using those values, we can also approximate whiskers. Assuming the outliers are not available, we can plot a rudimentary boxplot as follows.

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

# construct some data
x = np.random.default_rng(0).normal(size=(1000, 8))

means = x.mean(axis=0)
q1 = means + std * stats.norm.ppf(0.25)
q3 = means + std * stats.norm.ppf(0.75)
whislo = q1 - (q3 - q1)*1.5
whishi = q3 + (q3 - q1)*1.5

keys = ['med', 'q1', 'q3', 'whislo', 'whishi']
stats = [dict(zip(keys, vals)) for vals in zip(means, q1, q3, whislo, whishi)]
plt.subplot().bxp(stats, showfliers=False);

no fliers


Now assume we have access to median, first and third quartiles (median, q1, q3 below). Furthermore, let's assume we have access to values just below q1-(q3-q1)*1.5 and just above q3+(q3-q1)*1.5 which can be used to locate the whiskers. Also, let's assume we have access to values outside the whiskers (fliers). Then passing all this information to bxp() plots a graph that is the very same one that is plotted by plt.boxplot.

# construct some data
x = np.random.default_rng(0).normal(size=(1000, 8))
median = np.median(x, axis=0)
q1 = np.quantile(x, 0.25, axis=0)
q3 = np.quantile(x, 0.75, axis=0)

# compute whiskers' locations
whislo = [np.min(x[x[:, i] > v, i]) for i, v in enumerate(q1 - (q3 - q1)*1.5)]
whishi = [np.max(x[x[:, i] < v, i]) for i, v in enumerate(q3 + (q3 - q1)*1.5)]
# identify fliers
fliers = [x[(x[:, i] < lo) | (x[:, i] > hi), i] for i, (lo, hi) in enumerate(zip(whislo, whishi))]

keys = ['med', 'q1', 'q3', 'whislo', 'whishi', 'fliers']
stats = [dict(zip(keys, vals)) for vals in zip(median, q1, q3, whislo, whishi, fliers)]

plt.subplot().bxp(stats);

full boxplot

You can verify that the above plot is the very same plot that is plotted by

plt.boxplot(x);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.