首页 > 解决方案 > 从 matplotlib 箱线图中获取中值

问题描述

我正在使用 matplotlib 生成箱线图。为了创建绘图,箱线图类在内部计算均值、标准差和中位数。如何提取这些数值?

箱线图返回一个字典,其中包含对象。其中包括一个 Line2D 对象列表,位于键“medians”下,数据数组中的每个系列都有一个。

box = ax.boxplot(data, showmeans=True)
box
>>>{'whiskers': [<matplotlib.lines.Line2D at 0x2580149d0d0>,
  <matplotlib.lines.Line2D at 0x2580149d460>,
  <matplotlib.lines.Line2D at 0x258014a7a00>,
  <matplotlib.lines.Line2D at 0x258014a7d90>, ...etc.... ],
 'caps': [<matplotlib.lines.Line2D at 0x2580149d7f0>,
  <matplotlib.lines.Line2D at 0x2580149db80>,
  <matplotlib.lines.Line2D at 0x2580170a160>,
  <matplotlib.lines.Line2D at 0x2580170a4f0>, ...etc.... ],
 'boxes': [<matplotlib.lines.Line2D at 0x2580410fd00>,
  <matplotlib.lines.Line2D at 0x258014a7670>,
  <matplotlib.lines.Line2D at 0x2580170afa0>,
  <matplotlib.lines.Line2D at 0x25801708910>, ...etc.... ],
 'medians': [<matplotlib.lines.Line2D at 0x2580149df10>,
  <matplotlib.lines.Line2D at 0x2580170a880>,
  <matplotlib.lines.Line2D at 0x258017081f0>,
  <matplotlib.lines.Line2D at 0x25802263b20>, ...etc.... ],
 'fliers': [],
 'means': [<matplotlib.lines.Line2D at 0x258014a72e0>,
  <matplotlib.lines.Line2D at 0x2580170ac10>,
  <matplotlib.lines.Line2D at 0x25801708580>,
  <matplotlib.lines.Line2D at 0x25802263eb0>, ...etc.... ],}

有什么方法可以从绘图对象本身获取中值(也意味着标准偏差)?在某些情况下,这可能很有用,可以将其与我自己根据数据计算的值进行比较。

标签: pythonmatplotlib

解决方案


推荐阅读