1

I am trying to put in text into my boxplot. However after I arranged my x-axis in a specific order (superior, deluxe, family), the data labels are no longer following that order. How should I change my code to reflect that specific order?

Data:

    RoomCategory    ADR
514 Superior    114.75
515 Family  372.88
516 Deluxe  182.5
517 Deluxe  300.0
518 Family  371.45
519 Family  320.0
520 Deluxe  300.0
521 Family  414.0

My code:

box_plot = sns.boxplot(data=bgr21, x='RoomCategory',y='ADR',order=['Superior','Deluxe','Family'])

# printing the median value in the boxplot
medians= bgr21.groupby(['RoomCategory'])['ADR'].median()
vertical_offset = bgr21['ADR'].median() * 0.05 # offset from median for display
round_medians = np.round(medians,2)

for xtick in box_plot.get_xticks():
    box_plot.text(xtick,round_medians[xtick] + vertical_offset,round_medians[xtick], 
           horizontalalignment='center', size='large',color='b',weight='semibold')

Output:

Box plots in the order Superior, Deluxe, Family

Laurel
  • 5,965
  • 14
  • 31
  • 57
MH0563
  • 13
  • 3

1 Answers1

0

This order is not correct, and it is affected by the fact that we are specifying the order of the categories. So we need to reorder the median data to be annotated in the intended order. I used the order of the categories to sort the data.

import seaborn as sns

sns.set_style("whitegrid")
tips = sns.load_dataset("tips")
box_plot = sns.boxplot(x="day",y="total_bill",data=tips, order=['Sun','Sat','Fri','Thur'])

medians = tips.groupby(['day'])['total_bill'].median()
medians = medians.reindex(index=['Sun','Sat','Fri','Thur'], copy=True)
#print(medians)
vertical_offset = tips['total_bill'].median() * 0.05 # offset from median for display

for i in range(len(box_plot.get_xticklabels())):
    #print(i)
    box_plot.text(i,medians[i] + vertical_offset,medians[i], 
            horizontalalignment='center',size='x-small',color='w',weight='semibold')

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • hi, i've just tried to add some data into my question. does it help? thanks for your time. – MH0563 Dec 12 '21 at 13:44
  • The boxplot cannot be drawn with only the added data. It seems to me that your code is based on [this answer](https://stackoverflow.com/questions/38649501/labeling-boxplot-in-seaborn-with-median-value). I have changed the category order to this answer and sorted against it. – r-beginners Dec 12 '21 at 13:54
  • If you check the changes you made from the referenced answers, you will be able to see the modifications in your code. – r-beginners Dec 12 '21 at 13:55
  • hi, i got it. the secret is in ```medians.reindex(index=['Sun','Sat','Fri','Thur'], copy=True)``` to ensure the calculation starts from the sequence that i want. Is this the correct explanation? – MH0563 Dec 12 '21 at 13:59
  • That's exactly the point. – r-beginners Dec 12 '21 at 14:01