Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Labeling boxplot in seaborn with median value

How can I label each boxplot in a seaborn plot with the median value?

E.g.

import seaborn as sns sns.set_style("whitegrid") tips = sns.load_dataset("tips") ax = sns.boxplot(x="day", y="total_bill", data=tips) 

How do I label each boxplot with the median or average value?

like image 309
user308827 Avatar asked Jul 29 '16 02:07

user308827


People also ask

How do you show mean value in boxplot Seaborn?

How to Show mean marks on Boxplot with Seaborn? With Seaborn's boxplot() function, we can add a mark for mean values on the boxplot, using the argument “showmeans=True”. Seaborn's showmeans=True argument adds a mark for mean values in each box. By default, mean values are marked in green color triangles.

What is Orient in Seaborn?

orient“v” | “h”, optional. Orientation of the plot (vertical or horizontal). This is usually inferred based on the type of the input variables, but it can be used to resolve ambiguity when both x and y are numeric or when plotting wide-form data. colormatplotlib color, optional.


1 Answers

I love when people include sample datasets!

import seaborn as sns  sns.set_style("whitegrid") tips = sns.load_dataset("tips") box_plot = sns.boxplot(x="day",y="total_bill",data=tips)  medians = tips.groupby(['day'])['total_bill'].median() vertical_offset = tips['total_bill'].median() * 0.05 # offset from median for display  for xtick in box_plot.get_xticks():     box_plot.text(xtick,medians[xtick] + vertical_offset,medians[xtick],              horizontalalignment='center',size='x-small',color='w',weight='semibold') 

enter image description here

like image 73
mechanical_meat Avatar answered Sep 27 '22 16:09

mechanical_meat