栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

给定百分位值而不是原始输入,是否可以绘制matplotlib箱线图?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

给定百分位值而不是原始输入,是否可以绘制matplotlib箱线图?

为了只使用百分位值和离群值(如果有)绘制箱形图,我制作了一个

customized_box_plot
函数,该函数基本上修改了基本箱形图(由少量样本数据生成)中的属性,以使其根据您的百分位值进行拟合。

customized_box_plot
功能

def customized_box_plot(percentiles, axes, redraw = True, *args, **kwargs):    """    Generates a customized boxplot based on the given percentile values    """    box_plot = axes.boxplot([[-9, -4, 2, 4, 9],]*n_box, *args, **kwargs)     # Creates len(percentiles) no of box plots    min_y, max_y = float('inf'), -float('inf')    for box_no, (q1_start,       q2_start,      q3_start,      q4_start,      q4_end,      fliers_xy) in enumerate(percentiles):        # Lower cap        box_plot['caps'][2*box_no].set_ydata([q1_start, q1_start])        # xdata is determined by the width of the box plot        # Lower whiskers        box_plot['whiskers'][2*box_no].set_ydata([q1_start, q2_start])        # Higher cap        box_plot['caps'][2*box_no + 1].set_ydata([q4_end, q4_end])        # Higher whiskers        box_plot['whiskers'][2*box_no + 1].set_ydata([q4_start, q4_end])        # Box        box_plot['boxes'][box_no].set_ydata([q2_start,  q2_start,  q4_start, q4_start, q2_start])        # Median        box_plot['medians'][box_no].set_ydata([q3_start, q3_start])        # Outliers        if fliers_xy is not None and len(fliers_xy[0]) != 0: # If outliers exist box_plot['fliers'][box_no].set(xdata = fliers_xy[0],          ydata = fliers_xy[1]) min_y = min(q1_start, min_y, fliers_xy[1].min()) max_y = max(q4_end, max_y, fliers_xy[1].max())        else: min_y = min(q1_start, min_y) max_y = max(q4_end, max_y)        # The y axis is rescaled to fit the new box plot completely with 10%         # of the maximum value at both ends        axes.set_ylim([min_y*1.1, max_y*1.1])    # If redraw is set to true, the canvas is updated.    if redraw:        ax.figure.canvas.draw()    return box_plot

用法

使用逆逻辑(最后是代码),我从此示例中提取了百分位值

>>> percentiles(-1.0597368367634488, 0.3977683984966961, 1.0298955252405229, 1.6693981537742526, 3.4951447843464449)(-0.90494930553559483, 0.36916539612108634, 1.0303658700697103, 1.6874542731392828, 3.4951447843464449)(0.13744105279440233, 1.3300645202649739, 2.6131540656339483, 4.8763411136047647, 9.5751914834437937)(0.22786243898199182, 1.4120860286080519, 2.637650402506837, 4.9067126578493259, 9.4660357513550899)(0.0064696168078617741, 0.30586770128093388, 0.70774153557312702, 1.5241965711101928, 3.3092932063051976)(0.007009744579241136, 0.28627373934008982, 0.66039691869500572, 1.4772725266672091, 3.221716765477217)(-2.2621660374110544, 5.1901313713883352, 7.7178532139979357, 11.277744848353247, 20.155971739152388)(-2.2621660374110544, 5.1884411864079532, 7.3357079047721054, 10.792299385806913, 18.842012119715388)(2.5417888074435702, 5.885996170695587, 7.7271286220368598, 8.9207423361593179, 10.846938621419374)(2.5971767318505856, 5.753551925927133, 7.6569980004033464, 8.8161056254143233, 10.846938621419374)

请注意,为简短起见,我没有显示离群矢量,该向量将是每个百分位数组的第六个元素。

还要注意,可以使用所有其他常用的kwargs / args,因为它们可以简单地传递给其中的

boxplot
方法:

>>> fig, ax = plt.subplots()>>> b = customized_box_plot(percentiles, ax, redraw=True, notch=0, sym='+', vert=1, whis=1.5)>>> plt.show()

说明

boxplot
方法返回一个字典,该字典将箱线图的组成部分映射到
matplotlib.lines.Line2D
所创建的各个实例。

引用

matplotlib.pyplot.boxplot
文档:

该词典具有以下键(假定垂直框图):

框:框图的主体,显示四分位数和中位数的置信区间(如果启用)。

中位数:每个框的中位数处的水平线。

晶须:垂直线延伸到最极端的n个异常数据点。盖:晶须末端的水平线。

飞行物:表示数据的点超出了晶须(异常值)。

均值:代表均值的点或线。

例如观察

boxplot
的极小样本数据的
[-9, -4, 2, 4, 9]

>>> b = ax.boxplot([[-9, -4, 2, 4, 9],])>>> b{'boxes': [<matplotlib.lines.Line2D at 0x7fe1f5b21350>],'caps': [<matplotlib.lines.Line2D at 0x7fe1f54d4e50>,<matplotlib.lines.Line2D at 0x7fe1f54d0e50>],'fliers': [<matplotlib.lines.Line2D at 0x7fe1f5b317d0>],'means': [],'medians': [<matplotlib.lines.Line2D at 0x7fe1f63549d0>],'whiskers': [<matplotlib.lines.Line2D at 0x7fe1f5b22e10>,  <matplotlib.lines.Line2D at 0x7fe20c54a510>]}>>> plt.show()

这些

matplotlib.lines.Line2D
对象有两种方法,将在我的函数中广泛使用。
set_xdata
(或
set_ydata
)和
get_xdata
(或
get_ydata
)。

使用这些方法,我们可以更改基本框图的组成线的位置,以符合您的百分位值(这是

customized_box_plot
函数的作用)。更改组成线的位置后,您可以使用重新绘制画布
figure.canvas.draw()

总结了从百分位到各个

Line2D
对象的坐标的映射。

Y坐标:

  • 最大值(
    q4_end
    -第4个四分位数的结尾)对应于最上面的上限
    Line2D
    对象。
  • 最小值(
    q1_start
    -第一个四分位数的开始)对应于最低的上限
    Line2D
    对象。
  • 中位数对应于(
    q3_start
    )中位数
    Line2D
    对象。
  • 2个晶须位于盒子的两端和最高端盖之间(
    q1_start
    q2_start
    -下晶须
    q4_start
    q4_end
    -上晶须)
  • 盒子实际上是一条有趣的
    n
    形状,由下部的盖子围成的线。
    n
    异形线的端点对应于
    q2_start
    q4_start

X坐标:

  • 中心x坐标(对于多个箱形图,通常为1、2、3 …)
  • 该库根据指定的宽度自动计算边界x坐标。

从箱形图DICT中检索百分数的逆函数:

def get_percentiles_from_box_plots(bp):    percentiles = []    for i in range(len(bp['boxes'])):        percentiles.append((bp['caps'][2*i].get_ydata()[0],     bp['boxes'][i].get_ydata()[0],     bp['medians'][i].get_ydata()[0],     bp['boxes'][i].get_ydata()[2],     bp['caps'][2*i + 1].get_ydata()[0],     (bp['fliers'][i].get_xdata(),      bp['fliers'][i].get_ydata())))    return percentiles

注意:之所以没有制作完全自定义的箱形图方法,是因为内置箱形图提供了许多无法完全复制的功能。

如果我可能不必要地解释了一些过于明显的内容,也请原谅。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/625172.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号