Creating custom graphs with seaborn boxplot()

Einblick Content Team - March 17th, 2023

There are many different kinds of visualizations that are most effective for conveying different kinds of information. Box plots or box-and-whisker plots are particularly useful in comparing distributions of continuous variables across groups, and identifying outliers. In this post, we’ll use seaborn’s boxplot() function to create and customize different box plots.

Open and fork the canvas below for all example boxplots. We’ve used a subset of an Olympics dataset found on Kaggle, and will use it to examine the distribution of heights among athletes participating in different sports.

Import and setup

import seaborn as sns

We've loaded in our dataset as a pandas DataFrame called df.

Basic box plot: sns.boxplot(x or y)

The most basic box plot in seaborn will show the distribution of one continuous variable. You only need one argument: x or y. The variable you choose will alter the orientation of the box plot--whether it is horizontal or vertical.

NOTE: Alternatively you can use the data argument to specify where the entire dataset is stored, and then use the column name as a string to specify the value of x or y.

sns.boxplot(x = df["Height"])


Seaborn boxplot exampleSeaborn boxplot example
sns.boxplot(y = df["Height"])


Seaborn boxplot vertical exampleSeaborn boxplot vertical example

Advanced box plots: comparing groups

Comparing groups: Example 1 sns.boxplot(x and y, hue)

If you would like to compare the distributions of certain continuous variables across different categories of data, you can use a combination of the x, y, and hue arguments. While using just x and y, where one is continuous and the other is categorical, is sufficient, if you would like a legend, it is easiest to also set the hue argument equal to the categorical variable.

import matplotlib.pyplot as plt

sns.boxplot(data = df, x = "Height", y = "Sport", hue = "Sport", dodge = False)

# Adjust legend placement
plt.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0)


Seaborn boxplot hue variable exampleSeaborn boxplot hue variable example

If you would like to compare groups within groups, you can set hue to a different categorical variable than the one already present in the x and y dimensions, as below.

sns.boxplot(data = df, x = "Height", y="Sport", hue = "Sex")


Seaborn boxplot compare multiple groups exampleSeaborn boxplot compare multiple groups example


Einblick is an agile data science platform that provides data scientists with a collaborative workflow to swiftly explore data, build predictive models, and deploy data apps. Founded in 2020, Einblick was developed based on six years of research at MIT and Brown University. Einblick customers include Cisco, DARPA, Fuji, NetApp and USDA. Einblick is funded by Amplify Partners, Flybridge, Samsung Next, Dell Technologies Capital, and Intel Capital. For more information, please visit and follow us on LinkedIn and Twitter.

Start using Einblick

Pull all your data sources together, and build actionable insights on a single unified platform.

  • All connectors
  • Unlimited teammates
  • All operators