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.

# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
# set a grey background (use sns.set_theme() if seaborn version 0.11.0 or above) 
sns.set(style="darkgrid")
df = sns.load_dataset('iris')

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

Using the 'orient' argument

The orient argument can be useful when plotting a boxplot with x and y being both numerical variables, or when only specifying the 'data' argument, such as we did in the following example.

# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
# set a grey background (use sns.set_theme() if seaborn version 0.11.0 or above) 
sns.set(style="darkgrid")
df = sns.load_dataset('iris')

sns.boxplot(data=df[['sepal_length', 'petal_length']], orient='h')
plt.show()

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 🙏!