Python Seaborn 箱线图 : Overlay 95 percentile values on whisker

标签 python pandas matplotlib visualization seaborn

我想在seaborn箱线图上覆盖95个百分位值。我无法弄清楚覆盖文本的方法,或者是否有 Seaborn 的能力。我如何修改以下代码以在绘图上覆盖 95 个百分位数值。

import pandas as pd
import numpy as np
import seaborn as sns
df = pd.DataFrame(np.random.randn(200, 4), columns=list('ABCD'))*100
alphabet = list('AB')
df['Gr'] = np.random.choice(np.array(alphabet, dtype="|S1"), df.shape[0])
df_long = pd.melt(df, id_vars=['Gr'], value_vars = ['A','B','C','D'])
sns.boxplot(x = "variable", y="value", hue = 'Gr',  data=df_long, whis = [5,95])

最佳答案

考虑seaborn的plot.text,借用@bernie's answer (对于包含样本数据集也是一个健康的+1)。唯一的挑战是调整对齐方式,因为在色调字段中进行分组以使标签覆盖在每个箱线图系列上。甚至还有根据系列进行颜色编码的标签。

import pandas as pd
import numpy as np
import seaborn as sns

np.random.seed(61518)
# ... same as OP

# 95TH PERCENTILE SERIES
pctl95 = df_long.groupby(['variable', 'Gr'])['value'].quantile(0.95)
pctl95_labels = [str(np.round(s, 2)) for s in pctl95]

# GROUP INDEX TUPLES
grps = [(i, 2*i, 2*i+1) for i in range(4)]
# [(0,0,1), (1,2,3), (2,4,5), (3,6,7)]

pos = range(len(pctl95))

# ADJUST HORIZONTAL ALIGNMENT WITH MORE SERIES
for tick, label in zip(grps, hplot.get_xticklabels()):
    hplot.text(tick[0]-0.1, pctl95[tick[1]] + 0.95, pctl95_labels[tick[1]], 
               ha='center', size='x-small', color='b', weight='semibold')

    hplot.text(tick[0]+0.1, pctl95[tick[2]] + 0.95, pctl95_labels[tick[2]], 
               ha='center', size='x-small', color='g', weight='semibold')
sns.plt.show()

Plot Output

关于Python Seaborn 箱线图 : Overlay 95 percentile values on whisker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50879760/

相关文章:

python - 如何使用删除 NA 值的选项来融化 Pandas 中的数据框

python-3.x - python/pandas - 将列标题转换为索引

python - matplotlib:hist2d 中的日志转换计数

python - matplotlib 子图动画的奇怪行为

python - 如何为类提供类似 with-statement 的功能?

python - Spark 流作业性能改进

python - 没有匹配时打印空数据框

python - 在 python 中水平对齐条形图图例

python - 将 Python 函数传递给 Boost C++

python - Pandas 计算时间序列中的零点