Open In App

How to Show Mean on Boxplot using Seaborn in Python?

A boxplot is a powerful data visualization tool used to understand the distribution of data. It splits the data into quartiles, and summarises it based on five numbers derived from these quartiles:

It can also represent the symmetrically, skewness, and the spread of data.



In Python 3, We can graph a boxplot using three methods, using matplotlib, using pandas, or using seaborn. Here, we will use seaborn, which is a matplotlib wrapper that provides close integration with pandas data structures and better palette options than matplotlib. We will use seaborn.boxplot() method, and then we will learn how to show mean on boxplot. 



Step 1: Importing the libraries and loading the dataset




# importing useful libraries
import seaborn as sns
import matplotlib.pyplot as plt
 
# using titanic dataset from
# seaborn library
df = sns.load_dataset("titanic")
 
# to see first 5 rows of dataset
print(df.head())

 
 

First 5 rows of dataset

 

Step 2: plot a basic boxplot using seaborn.boxplot()

 




# to plot a boxplot of
# age vs survived feature
plt.figure(figsize=(10, 8))
sns.boxplot(x='survived',
            y='age',
            data=df)
plt.ylabel("Age", size=14)
plt.xlabel("Survived", size=14)
plt.title("Titanic Dataset", size=18)

 

 

We observe that median is shown as a quartile line, but the mean is not shown.

 

simple boxplot

 

Step 3: To show mean, we use an extra keyword argument in the boxplot function. We set showmeans as True.

 




# boxplot with showmeans
plt.figure(figsize=(10, 8))
sns.boxplot(x='survived',
            y='age',
            data=df,
            showmeans=True# notice the change
plt.ylabel("Age", size=14)
plt.xlabel("Survived", size=14)
plt.title("Titanic Dataset", size=18)

 

 

Now, we observe that the mean is marked as a green triangle, which doesn’t go well with our color scheme.

 

plotting mean using showmeans keyword argument

 

Step 4: To set up our customized marker and markercolor, we will use ‘meanprops’ keyword argument as shown in the code below.

 




# customizing using meanprops
plt.figure(figsize=(10, 8))
sns.boxplot(x='survived',
            y='age',
            data=df,
            showmeans=True,
            meanprops={"marker": "+",
                       "markeredgecolor": "black",
                       "markersize": "10"})
plt.ylabel("Age", size=14)
plt.xlabel("Survived", size=14)
plt.title("Titanic Dataset", size=18)



Article Tags :