Horizontal boxplot with Seaborn

logo of a chart:Box1

It is quite straightforward to turn your boxplot horizontal with seaborn. To do so, you may either switch your x and y arguments, or use the option orient='h'

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.

🚨 Grab the Data To Viz poster!


Do you know all the chart types? Do you know which one you should pick? I made a decision tree that answers those questions. You can download it for free!

    dataviz decision tree poster