Rotating a histogram with Seaborn

logo of a chart:Histogram

It is quite straightforward to rotate your histogram by 90 degrees with Seaborn: just switch the way you would assign x and y arguments inside the histogram function.

Libraries

First, we need to import the following libraries:

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.

🚨 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