Python 使用预计算(汇总)统计数据的Matplotlib箱线图

Python 使用预计算(汇总)统计数据的Matplotlib箱线图,python,matplotlib,boxplot,Python,Matplotlib,Boxplot,我需要做一个箱线图(在Python和matplotlib中),但我没有原始的“原始”数据。 我得到的是最大值、最小值、平均值、中值和IQR(正态分布)的预先计算值,但我仍然想做一个箱线图。当然,绘制异常值是不可能的,但除此之外,我想所有信息都在那里 我到处寻找答案,但没有成功。我最接近的问题是同一个问题,但对于R(我不熟悉)。看 有人能告诉我怎么做箱线图吗 非常感谢 在旧版本中,必须通过单独更改箱线图元素手动执行此操作: Mean=[3.4] #mean IQR=[3.0,3.9] #inter

我需要做一个箱线图(在Python和matplotlib中),但我没有原始的“原始”数据。 我得到的是最大值、最小值、平均值、中值和IQR(正态分布)的预先计算值,但我仍然想做一个箱线图。当然,绘制异常值是不可能的,但除此之外,我想所有信息都在那里

我到处寻找答案,但没有成功。我最接近的问题是同一个问题,但对于R(我不熟悉)。看

有人能告诉我怎么做箱线图吗


非常感谢

在旧版本中,必须通过单独更改箱线图元素手动执行此操作:

Mean=[3.4] #mean
IQR=[3.0,3.9] #inter quantile range
CL=[2.0,5.0] #confidence limit
A=np.random.random(50)
D=plt.boxplot(A) # a simple case with just one variable to boxplot
D['medians'][0].set_ydata(Mean)
D['boxes'][0]._xy[[0,1,4], 1]=IQR[0]
D['boxes'][0]._xy[[2,3],1]=IQR[1]
D['whiskers'][0].set_ydata(np.array([IQR[0], CL[0]]))
D['whiskers'][1].set_ydata(np.array([IQR[1], CL[1]]))
D['caps'][0].set_ydata(np.array([CL[0], CL[0]]))
D['caps'][1].set_ydata(np.array([CL[1], CL[1]]))
_=plt.ylim(np.array(CL)+[-0.1*np.ptp(CL), 0.1*np.ptp(CL)]) #reset the limit

多亏@tacaswell的评论,我找到了所需的文档,并使用Matplotlib 1.4.3给出了一个示例。 但是,此示例不会自动将地物缩放到正确的大小

import matplotlib.pyplot as plt

item = {}

item["label"] = 'box' # not required
item["mean"] = 5 # not required
item["med"] = 5.5
item["q1"] = 3.5
item["q3"] = 7.5
#item["cilo"] = 5.3 # not required
#item["cihi"] = 5.7 # not required
item["whislo"] = 2.0 # required
item["whishi"] = 8.0 # required
item["fliers"] = [] # required if showfliers=True

stats = [item]

fig, axes = plt.subplots(1, 1)
axes.bxp(stats)
axes.set_title('Default')
y_axis = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y_values = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
plt.yticks(y_axis, y_values)
文件的相关链接:


参考@MKroehnert和
箱线图抽屉功能的答案,以下内容可能会有所帮助:

import matplotlib.pyplot as plt

stats = [{
    "label": 'A',  # not required
    "mean":  5,  # not required
    "med": 5.5,
    "q1": 3.5,
    "q3": 7.5,
    # "cilo": 5.3 # not required
    # "cihi": 5.7 # not required
    "whislo": 2.0,  # required
    "whishi": 8.0,  # required
    "fliers": []  # required if showfliers=True
    }]

fs = 10  # fontsize

fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(6, 6), sharey=True)
axes.bxp(stats)
axes.set_title('Boxplot for precalculated statistics', fontsize=fs)
plt.show()

此功能存在于主分支上,将在1.4中提供(应标记为“很快”)。这里不需要任何特殊函数-只需使用常规matplotlib boxplot函数,因为如果整个数据集仅包含例如最小值、q1、中值、q3和最大值,那么当它计算数据集上的汇总统计数据时,它们将是那些精确的点!试试看。对于像我一样在这个问题上遇到困难的人:matplotlib网站上也有一些详细的文档:对于遇到困难并对语法感到失望的人:现在有更简单的方法可以做到这一点,请参阅matplotlib文档: