Python 获取方框图的数据-Matplotlib

Python 获取方框图的数据-Matplotlib,python,matplotlib,boxplot,Python,Matplotlib,Boxplot,我必须绘制一些数据的箱线图,我可以用Matplotlib轻松地绘制。然而,我被要求提供一个表格,其中包含了数据,比如胡须、中间值、标准偏差等等 我知道我可以“手工”计算这些,但从参考资料中我也知道,boxplot方法: Returns a dictionary mapping each component of the boxplot to a list of the matplotlib.lines.Line2D instances created. That dictionary has t

我必须绘制一些数据的箱线图,我可以用Matplotlib轻松地绘制。然而,我被要求提供一个表格,其中包含了数据,比如胡须、中间值、标准偏差等等

我知道我可以“手工”计算这些,但从参考资料中我也知道,
boxplot
方法:

Returns a dictionary mapping each component of the boxplot to a list of the matplotlib.lines.Line2D instances created. That dictionary has the following keys (assuming vertical boxplots):

boxes: the main body of the boxplot showing the quartiles and the median’s confidence intervals if enabled.
medians: horizonal lines at the median of each box.
whiskers: the vertical lines extending to the most extreme, n-outlier data points.
caps: the horizontal lines at the ends of the whiskers.
fliers: points representing data that extend beyone the whiskers (outliers).
所以我想知道如何获得这些值,因为它们是matplotlib.lines.Line2D


谢谢。

如您所知,您需要访问boxplot返回值的成员

即,例如,如果返回值存储在
bp

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

>> array([ 2.5,  2.5])
由于箱线图是垂直的,因此中间线是水平线,您只需要关注其中一个y值;i、 e.我的样本数据的中位数为2.5

对于字典中的每个“键”,该值将是一个列表,用于处理多个框。如果只有一个箱线图,列表将只有一个元素,因此我使用了上面的
bp['medians']
[0]。 如果箱线图中有多个框,则需要使用例如

for medline in bp['medians']:
    linedata = medline.get_ydata()
    median = linedata[0]
不幸的是,CT Zhu的答案不起作用,因为不同的元素表现不同。另外,例如,只有一个中位数,但有两个胡须……因此,按照上述方法手动处理每个数量是最安全的

注意:你能来的最近的地方是:

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

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

“手工”计算这些有什么问题?大部分信息可以通过
numpy
轻松获得,例如
np.median
,或
np.min
np.max
@BenjaminBannier:特别是对于大型数据集,这是重复工作,因为在创建箱线图时已经调用了成本更高的函数,例如
median
,@jmetz!由于这些原因,我想知道如何从已经计算的方框图中获取这些值。@pceccon,是的,这很有意义。谢谢!在
bp['medians'][0]
中,为什么要使用[0]?如果我只有一个方框图,这与
bp['medians']
不一样吗?为什么会返回两个值的数组?我问这个问题是因为我想知道我是要看第一个还是第二个,或者他们是不是你一样,这似乎没有意义。是的,你是对的,结果是CT Zhu的答案根本不起作用:/认为你必须处理每个项目(中位数、大写、方框等)separately@pceccon:我错误地认为朱CT的答案是正确的;没有。我已经相应地更新了我的答案。谢谢您的时间,@jmetz。一个长方体有4条边,绘制时有5个节点。(起点节点在终点重复)。