How to Show Mean on Boxplot using Seaborn in Python?

The mean is generally added to boxplots as a point inside of the box with a line going over it. Alternatively, the yellow shaded area can be replaced by another line or symbol. For analysis purposes, it helps to be able to add these lines and symbols to an existing boxplot in Seaborn.

show avg value in sns boxplot

By Thankful TigerThankful Tiger on May 23, 2020
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')

Source: stackoverflow.com

Add Comment

0

It would also make sense to give that option its own spot in the list_plot function rather than forcing people to go about it in varying ways.

Python answers related to "show avg value in sns boxplot"

View All Python queries

Python queries related to "show avg value in sns boxplot"

Browse Other Code Languages

CodeProZone