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

北战南征 提交于 2020-07-27 23:21:06

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/54033076/can-i-generate-a-boxplot-without-a-dataset-and-only-having-the-relevant-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!