Libraries
First, we need to load a few libraries:
- matplotlib: for displaying the chart
- seaborn: for creating the chart
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.