median = 3637
std = 1274.997414
perc_25 = 2627.0
perc_75 = 4238.0
我有 4 个从数据中得出的值。我怎样才能用这个制作箱线图?我期望一条线代表中位数,一个由 25 百分位数和 75 百分位数分隔的框,每边各有一个点表示中位数 + 标准差和中位数 - 标准差。
通常我需要一个值列表或一个数据框,但我已经计算了统计数据,现在我只想显示它们。
在内部,
boxplot
使用bxpstats
(source)计算
matplotlib.cbook.boxplot_stats
,然后将结果传递给Axes.bxp
(source)。
bxpstats = cbook.boxplot_stats(x, whis=whis, bootstrap=bootstrap,
labels=labels, autorange=autorange)
...
artists = self.bxp(bxpstats, positions=positions, widths=widths,
vert=vert, patch_artist=patch_artist,
shownotches=notch, showmeans=showmeans,
showcaps=showcaps, showbox=showbox,
boxprops=boxprops, flierprops=flierprops,
medianprops=medianprops, meanprops=meanprops,
meanline=meanline, showfliers=showfliers,
capprops=capprops, whiskerprops=whiskerprops,
manage_ticks=manage_ticks, zorder=zorder,
capwidths=capwidths)
您可以通过设计正确格式的字典来简化第一步:
import matplotlib.pyplot as plt
median = 3637
std = 1274.997414
perc_25 = 2627.0
perc_75 = 4238.0
IQR = perc_75-perc_25
bxpstats = [{'mean': 2.0,
'iqr': IQR,
'whishi': IQR+perc_75,
'whislo': IQR-perc_25,
'fliers': [],
'q1': perc_25,
'med': median,
'q3': perc_75}]
ax = plt.subplot()
ax.bxp(bxpstats)
输出: