【问题标题】:Connect quantiles in side-by-side boxplot在并排箱线图中连接分位数
【发布时间】:2023-01-23 23:19:39
【问题描述】:

我有一个像这样的并排箱线图

import pandas as pd
import numpy as np
group = np.array([  np.random.binomial(2,0.4)   for _ in range(100)])
data = [  np.random.uniform(0,5)   for _ in range(len(group))]
df = pd.DataFrame({'data': data, 'group': group} )
df.boxplot(by='group', vert=False)

我试图将这些箱线图的 25、50 和 75 分位数连接在一起,我想要的输出是这样的:

我想知道是否有办法用 pandas/matplotlib 做到这一点

【问题讨论】:

    标签: pandas matplotlib boxplot


    【解决方案1】:

    您可以使用:

    ax = df.boxplot(by='group', vert=False)
    
    quantiles = df.groupby('group')['data'].quantile([0.25, 0.5, 0.75]).unstack()
    for col in quantiles:
        ax.plot(quantiles[col], range(1, len(quantiles)+1), label=col)
    plt.legend()
    

    如果您有垂直箱线图,这会更容易一些:

    ax = df.boxplot(by='group', vert=True)
    
    quantiles = df.groupby('group')['data'].quantile([0.25, 0.5, 0.75]).unstack()
    quantiles.set_axis(range(1, len(quantiles)+1)).plot(ax=ax)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 1970-01-01
      • 2018-09-30
      • 2017-09-12
      • 2016-11-19
      • 1970-01-01
      相关资源
      最近更新 更多