python中用箱线图分析异常值_python - 使用Matplotlib和异常值手动绘制箱线图 - 堆栈内存溢出...

该问题给出的答案是针对多个箱形图,我修改了代码以适合单个箱形图。

这是我尝试的代码:

def custom_boxplot(mini, q1, q2, q3, maxm, *outliers):

"""

Input:

Five-number summary separated into different arguments;

The following arguments after the summary are the outliers.

Output:

A boxplot drawn in the console.

"""

figure = plt.figure(figsize=(8,8))

ax = plt.gca()

bp = plt.boxplot([mini, q1, q2, q3, maxm])

fliers = bp['fliers']

for v in outliers:

fliers[0].set(xdata = 1, ydata = v)

_all = [mini, q1, q2, q3, maxm] + list(outliers)

_min, _max = min(_all), max(_all)

ax.set_ylim([_min*0.9, _max*1.1])

figure.canvas.draw()

但是,当我尝试运行以下行时

custom_boxplot(43.2, 43.5, 51.05, 56.8, 69.3, 13.8, 21.2)

它输出的盒形图仅包含最后一个参数的一个异常值。 我希望在箱图中为13.8和21.2绘制两个离群值的数据点。

我相信错误在这里附近:

...

for v in outliers:

fliers[0].set(xdata = 1, ydata = v)

...

我知道,因为我只有一个箱形图,所以可以像fliers[0]那样进行下标,以从箱形图获得第一个箱形图。 xdata = 1因为我再次为第一个框设置了此值,然后为ydata=v设置了离群值的y值。 我的代码中的错误在哪里?

你可能感兴趣的:(python中用箱线图分析异常值_python - 使用Matplotlib和异常值手动绘制箱线图 - 堆栈内存溢出...)