gpt4 book ai didi

python-3.x - Python : How to print the box, 盒须图中的 mustache 和离群值?

转载 作者:行者123 更新时间:2023-12-04 16:02:30 26 4
gpt4 key购买 nike

我已经为我的数据绘制了一个箱线图

我的代码:

red_diamond = dict(markerfacecolor='r', marker='D')
fig3, ax3 = plt.subplots()
ax3.set_title('Changed Outlier Symbols')
ax3.boxplot(maximum.values[:,1], flierprops=red_diamond)

我得到了一个情节如下:
enter image description here

我想做什么:在图本身上打印 mustache 、异常值(红色菱形)、四分位数和中位数的值。

最佳答案

ax.boxplot 返回一个字典,其中包含在制作盒须图时绘制的所有线条。一种选择是查询这本词典,并根据它包含的信息创建标签。相关的键是:

  • boxes IQR
  • medians为中位数
  • caps用于 mustache
  • fliers对于异常值

  • 请注意,下面的函数仅适用于单个箱线图(如果您一次创建多个箱,则需要更加小心地从字典中获取信息)。

    另一种方法是从数据数组本身中查找信息(查找中位数和 IQR 很容易)。我不确定 matplotlib 究竟如何确定传单是什么以及大写字母应该放在哪里。如果你想这样做,修改下面的函数应该很容易。
    import matplotlib.pyplot as plt
    import numpy as np

    # Make some dummy data
    np.random.seed(1)
    dummy_data = np.random.lognormal(size=40)

    def make_labels(ax, boxplot):

    # Grab the relevant Line2D instances from the boxplot dictionary
    iqr = boxplot['boxes'][0]
    caps = boxplot['caps']
    med = boxplot['medians'][0]
    fly = boxplot['fliers'][0]

    # The x position of the median line
    xpos = med.get_xdata()

    # Lets make the text have a horizontal offset which is some
    # fraction of the width of the box
    xoff = 0.10 * (xpos[1] - xpos[0])

    # The x position of the labels
    xlabel = xpos[1] + xoff

    # The median is the y-position of the median line
    median = med.get_ydata()[1]

    # The 25th and 75th percentiles are found from the
    # top and bottom (max and min) of the box
    pc25 = iqr.get_ydata().min()
    pc75 = iqr.get_ydata().max()

    # The caps give the vertical position of the ends of the whiskers
    capbottom = caps[0].get_ydata()[0]
    captop = caps[1].get_ydata()[0]

    # Make some labels on the figure using the values derived above
    ax.text(xlabel, median,
    'Median = {:6.3g}'.format(median), va='center')
    ax.text(xlabel, pc25,
    '25th percentile = {:6.3g}'.format(pc25), va='center')
    ax.text(xlabel, pc75,
    '75th percentile = {:6.3g}'.format(pc75), va='center')
    ax.text(xlabel, capbottom,
    'Bottom cap = {:6.3g}'.format(capbottom), va='center')
    ax.text(xlabel, captop,
    'Top cap = {:6.3g}'.format(captop), va='center')

    # Many fliers, so we loop over them and create a label for each one
    for flier in fly.get_ydata():
    ax.text(1 + xoff, flier,
    'Flier = {:6.3g}'.format(flier), va='center')

    # Make the figure
    red_diamond = dict(markerfacecolor='r', marker='D')
    fig3, ax3 = plt.subplots()
    ax3.set_title('Changed Outlier Symbols')

    # Create the boxplot and store the resulting python dictionary
    my_boxes = ax3.boxplot(dummy_data, flierprops=red_diamond)

    # Call the function to make labels
    make_labels(ax3, my_boxes)

    plt.show()

    enter image description here

    关于python-3.x - Python : How to print the box, 盒须图中的 mustache 和离群值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55648729/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com