Box marking with median values

In addition to the solution posted in this link , I would also like if I could add a Hue Parameter and add median values ​​at each of the plots.

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 an output like: enter image description here

I am working on the Titanic dataset which can be found in this link .

I am getting the values ​​I want, but only when I make a print expression, how do I include it in my graph?

+3


source to share


1 answer


Place your labels manually according to the hue and width of the bars for each category in a loop 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()

      



enter image description here

+3


source







All Articles