Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 932
Ray Dey Avatar asked Mar 04 '23 19:03

Ray Dey


1 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

like image 86
busybear Avatar answered Mar 08 '23 13:03

busybear