Libraries

First, we need to load a few libraries:

import seaborn as sns
import matplotlib.pyplot as plt

Dataset

Since boxplot is used to display the distribution of numerical variables, we need a dataset that contains at least one numerical variable.

In this example, we will use the iris dataset that we can easily load:

df = sns.load_dataset('iris')

Switching x and y

Notice how the orientation of the boxplot only depends on how you declare the numerical variable you expect to analyze.

If set as the argument y, the boxplot will be vertical, and if set as x, it will be horizontal.

sns.set_theme(style="darkgrid")
sns.boxplot(y=df["species"], x=df["sepal_length"])
plt.show()

Going further

This post explains how to flip a boxplot with the seaborn library.

You might be interested in how adding individual data points in boxplot and how to create a raincloud plot with the ptitprince library.

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!