Can I generate a boxplot without a dataset and only having the relevant values (median, quartiles, etc) in matplotlib?

Question:

I have calculated medians, lower/upper quartiles, minimum and maximum values as part of a separate application of mine and written those values to a file and I would like to construct boxplots using these specific values.

Is there a way using matplotlib (or seaborn) to manually specify each of these values instead of providing an array of data (I don’t need outliers)? I’ve tried looking through the documentation and I haven’t found anything particularly relevant, but I may have overlooked something.

Something like:

plt.boxplot(median=median_val, quartiles=(lower, upper), range=(min, max))

would be ideal. I’m also open to any web solutions too.

Asked By: Ray Dey

||

Answers:

There’s a baked-in function in matplotlib–bxp–that let’s you specify the calculated statistics rather than the raw data to calculate from, avoiding the need to create your own function.

You’ll need to call it as a method from your Axes object rather than from plt:

import matplotlib.pyplot as plt

stats = [
    {'med': 5, 'q1': 2, 'q3': 6, 'whislo': 1, 'whishi': 8},
    {'med': 4, 'q1': 2, 'q3': 6, 'whislo': 1, 'whishi': 8}
]

_, ax = plt.subplots();
ax.bxp(stats, showfliers=False);

Importantly, your input needs to be a list of dictionaries, corresponding to a list of boxes (even if just 1) to draw.

enter image description here

Answered By: busybear