Libraries
First, we need to import the following libraries:
- matplotlib for customizing the chart
- seaborn for creating the chart
import seaborn as sns
import matplotlib.pyplot as plt
Dataset
We'll use the iris dataset that is available using the load_dataset()
function from seaborn
df = sns.load_dataset("iris")
Flipped histogram
Flipping a histogram with seaborn is easy: you just have to specify that you want to display it on the y axis
.
sns.set_theme(style="darkgrid")
# Create the plot
sns.histplot(data=df, y="sepal_length")
plt.show()
Flipped histogram with density estimation
If you want to add the density estimation, you have to add kde=True
in the histplot
function
sns.set_theme(style="darkgrid")
# Create the plot
sns.histplot(data=df, y="sepal_length", kde=True)
plt.show()
Going further
This post explains how to create a flipped histogram with seaborn.
You might be interested in creating histogram with multiple groups or controlling the bins.