用中间值标记箱线图 [英] Labeling boxplot with median values

查看:114
本文介绍了用中间值标记箱线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了



我正在研究《泰坦尼克号》数据集,该数据集可在此链接



我获得了必需的值,但仅当我执行打印时声明,如何将其包括在情节中?

解决方案

在所有xticklabel周期中,根据色相参数和每个类别的条形宽度手动放置标签:

 进口seaborn as sns 
进口熊猫as pd
进口numpy as np
进口matplotlib.pylab为plt

trainData = pd.read_csv('titanic.csv')
testPlot = sns.boxplot(x ='pclass',y ='age',hue ='sex',数据= trainData)
m1 = trainData.groupby(['pclass','sex'])['age']。median()。values
mL1 = [str(np.round(s,2 ))对于m中的s]

ind = 0
对于范围内的刻度(len(testPlot.get_xticklabels())):
testPlot.text(tick-.2, m1 [ind + 1] + 1,mL1 [ind + 1],horizo​​ntalalignment ='center',color ='w',weight ='semibold')
testPlot.text(tick + .2,m1 [ind] + 1,mL1 [ind],horizo​​ntalalignment ='center',color ='w',weight ='semibold')
ind + = 2
plt.show()

< img src = https ://i.stack.imgur.com/PSq9F.png alt =在此处输入图片描述>


In addition to the solution posted in this link I would also like if I can also add the Hue Parameter, and add the Median Values in each of the plots.

The Current Code:

testPlot = sns.boxplot(x='Pclass', y='Age', hue='Sex', data=trainData)
m1 = trainData.groupby(['Pclass', 'Sex'])['Age'].median().values
mL1 = [str(np.round(s, 2)) for s in m1]
p1 = range(len(m1))

for tick, label in zip(p1, testPlot.get_xticklabels()):
    print(testPlot.text(p1[tick], m1[tick] + 1, mL1[tick]))

Gives a Output Like:

I'm working on the Titanic Dataset which can be found in this link.

I'm getting the required values, but only when I do a print statement, how do I include it in my Plot?

解决方案

Place your labels manually according to hue parameter and width of bars for every category in a cycle of all xticklabels:

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pylab as plt

trainData = pd.read_csv('titanic.csv')
testPlot = sns.boxplot(x='pclass', y='age', hue='sex', data=trainData)
m1 = trainData.groupby(['pclass', 'sex'])['age'].median().values
mL1 = [str(np.round(s, 2)) for s in m1]

ind = 0
for tick in range(len(testPlot.get_xticklabels())):
    testPlot.text(tick-.2, m1[ind+1]+1, mL1[ind+1],  horizontalalignment='center',  color='w', weight='semibold')
    testPlot.text(tick+.2, m1[ind]+1, mL1[ind], horizontalalignment='center', color='w', weight='semibold')
    ind += 2    
plt.show()

这篇关于用中间值标记箱线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆