Python-Matplotlib 箱线图。如何显示百分位数 0、10、25、50、75、90 和 100?

标签 python matplotlib boxplot percentile

我想绘制一个 EPSgram (见下文)使用 Python 和 Matplotlib。

boxplot函数仅绘制四分位数(0、25、50、75、100)。那么,我怎样才能再添加两个框呢?

EPSGram boxplot

最佳答案

如果您仍然好奇,我整理了一个样本。它使用 scipy.stats.scoreatpercentile ,但您可能从其他地方获得这些数字:

from random import random
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import scoreatpercentile

x = np.array([random() for x in xrange(100)])

# percentiles of interest
perc = [min(x), scoreatpercentile(x,10), scoreatpercentile(x,25),
               scoreatpercentile(x,50), scoreatpercentile(x,75),
               scoreatpercentile(x,90), max(x)]
midpoint = 0 # time-series time

fig = plt.figure()
ax = fig.add_subplot(111)
# min/max
ax.broken_barh([(midpoint-.01,.02)], (perc[0], perc[1]-perc[0]))
ax.broken_barh([(midpoint-.01,.02)], (perc[5], perc[6]-perc[5]))
# 10/90
ax.broken_barh([(midpoint-.1,.2)], (perc[1], perc[2]-perc[1]))
ax.broken_barh([(midpoint-.1,.2)], (perc[4], perc[5]-perc[4]))
# 25/75
ax.broken_barh([(midpoint-.4,.8)], (perc[2], perc[3]-perc[2]))
ax.broken_barh([(midpoint-.4,.8)], (perc[3], perc[4]-perc[3]))

ax.set_ylim(-0.5,1.5)
ax.set_xlim(-10,10)
ax.set_yticks([0,0.5,1])
ax.grid(True)
plt.show()

Output of the code above

关于Python-Matplotlib 箱线图。如何显示百分位数 0、10、25、50、75、90 和 100?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8681199/

相关文章:

python - Pandas 分组加权累计总和

python - 使用 matplotlib.pyplot (Python) 绘制椭圆

python - 在 plt 之外设置 matplotlib 颜色图的限制

python - 单击时无事件连接

python - 如何计算每组中最常见的组合

python - PyQt5 从主模块外部实时登录 QTextedit

python - 减去分组数据框中的行

r - 带有中值标签的箱线图和下面的 n 表

python - Pandas 中的加权箱线图

x 轴上的 python/matplotlib 箱线图