如何将四分位数的中位数信息与箱形图对齐

时间:2019-04-09 17:18:19

标签: matplotlib seaborn

我正在创建一个箱形图,并希望有中位数和分位数的标签。我还指定了要显示图的顺序。该图按特定顺序显示,但中位数和分位数信息未显示。

我已在其他箱形图中成功使用了此代码,使用了不同的列名,但不适用于这些特定的列

a4_dims = (11.7, 8.27)
fig, ax = plt.subplots(figsize=a4_dims)
sns.boxplot(x='2017Seg', y='AbsYr1Dif', data=yr1, width=.7, showfliers=False,
           order=["<10","10-19","20-49","50-99","100-249","250+"])

medians = yr1.groupby(['2017Seg'])['AbsYr1Dif'].median().values
median_labels = [str(np.round(s, 2)) for s in medians]

pos = range(len(medians))
for tick,label in zip(pos,ax.get_xticklabels()):
    ax.text(pos[tick], medians[tick] + 0.02, median_labels[tick], 
            horizontalalignment='center', size='small', color='w', weight='semibold')

    upperquart = yr1.groupby(['2017Seg'])['AbsYr1Dif'].quantile(.75).values
upperquart_labels = [str(np.round(s, 2)) for s in upperquart]

pos = range(len(upperquart))
for tick,label in zip(pos,ax.get_xticklabels()):
    ax.text(pos[tick], upperquart[tick] + 0.02, upperquart_labels[tick], 
            horizontalalignment='left', size='small', color='b', weight='semibold')

lowerquart = yr1.groupby(['2017Seg'])['AbsYr1Dif'].quantile(.25).values
lowerquart_labels = [str(np.round(s, 2)) for s in lowerquart]

pos = range(len(lowerquart))
for tick,label in zip(pos,ax.get_xticklabels()):
    ax.text(pos[tick], lowerquart[tick] - 0.05, lowerquart_labels[tick], 
            horizontalalignment='right', size='small', color='b', weight='semibold')

无法使用截图工具。

中位数和分位数信息的出现顺序是在箱线图定义上使用排序之前的自然排序顺序

无法正常工作的数据示例:

Sample of data not working

正在工作的数据示例:

Sample Data that previously worked

箱线图不起作用-中位数/分位数混乱

Boxplot not working - median / quantile out of order

0 个答案:

没有答案