Getting data of a box plot - Matplotlib

冷暖自知 提交于 2019-12-04 12:01:57

As you've figured out, you need to access the members of the return value of boxplot.

Namely, e.g. if your return value is stored in bp

bp['medians'][0].get_ydata()

>> array([ 2.5,  2.5])

As the boxplot is vertical, and the median line is therefore a horizontal line, you only need to focus on one of the y-values; i.e. the median is 2.5 for my sample data.

For each "key" in the dictionary, the value will be a list to handle for multiple boxes. If you have just one boxplot, the list will only have one element, hence my use of bp['medians'][0] above. If you have multiple boxes in your boxplot, you will need to iterate over them using e.g.

for medline in bp['medians']:
    linedata = medline.get_ydata()
    median = linedata[0]

CT Zhu's answer doesn't work unfortunately, as the different elements behave differently. Also e.g. there's only one median, but two whiskers...therefore it's safest to manually treat each quantity as outlined above.

NB the closest you can come is the following;

res  = {}
for key, value in bp.items():
    res[key] = [v.get_data() for v in value]

or equivalently

res = {key : [v.get_data() for v in value] for key, value in bp.items()}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!