0

我有一组表格列表(大约 100 个),[6, 17, 5, 1, 4, 7, 14, 19, 0, 10]我想得到一个箱线图,它绘制所有列表的箱线图信息(即中位数、最大值、最小值、Q1、Q3、异常值)的平均值。

例如,如果我有 2 个列表

l1 = [6, 17, 5, 1, 4, 7, 14, 19, 0, 10]
l2 = [4, 12, 3, 5, 16, 0, 14, 7, 8, 15]

我可以得到列表的最大值、中值和最小值的平均值,如下所示:

maxs = np.array([])
mins = np.array([])
medians = np.array([])
for l in [l1, l2]:
    medians = np.append(medians, np.median(l))
    maxs = np.append(maxs, np.max(l))
    mins = np.append(mins, np.min(l))
averMax = np.mean(maxs)
averMin = np.mean(mins)
averMedian = np.mean(medians)

我应该对箱形图中的其他信息执行相同的操作,例如平均 Q1、平均 Q3。然后,我需要使用这些信息(averMax、averMin 等)仅绘制一个箱形图(而不是一个图中的多个箱形图)。

我从使用 matplotlib 绘制箱线图知道您不必计算普通箱线图的值。您只需要将数据指定为变量。是否可以对我的情况做同样的事情,而不是手动计算所有列表的平均值?

4

1 回答 1

0

pd.describe()将获得四分位数,因此您可以根据它们制作图表。我在这个答案官方参考的示例图的帮助下定制了计算出来的数字。

import pandas as pd
import numpy as np
import io

l1 = [6, 17, 5, 1, 4, 7, 14, 19, 0, 10]
l2 = [4, 12, 3, 5, 16, 0, 14, 7, 8, 15]

df = pd.DataFrame({'l1':l1, 'l2':l2}, index=np.arange(len(l1)))

df.describe()
l1  l2
count   10.000000   10.000000
mean    8.300000    8.400000
std 6.532823    5.561774
min 0.000000    0.000000
25% 4.250000    4.250000
50% 6.500000    7.500000
75% 13.000000   13.500000
max 19.000000   16.000000

import matplotlib.pyplot as plt

# spread,center, filer_high, flier_low
x1 = [l1[4]-1.5*(l1[6]-l1[4]), l1[4], l1[5], l1[5]+1.5*(l1[6]-l1[4])]
x2 = [l2[4]-1.5*(l2[6]-l2[4]), l2[4], l2[5], l2[5]+1.5*(l2[6]-l2[4])]

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

plt.boxplot([x for x in [x1, x2]], 0, 'rs', 1)
plt.xticks([y+1 for y in range(len([x1, x2]))], ['x1', 'x2'])
plt.xlabel('measurement x')
t = plt.title('Box plot')
plt.show()

在此处输入图像描述

于 2020-09-03T10:18:28.953 回答