How to plot a histogram but split it by categories using Seaborn in Python

1 min

It might come in handy to plot a histogram to visualize our dataset but split it by a certain category.

Here we use the example of irises species

import seaborn as sns

# We set the theme
sns.set_theme(style="darkgrid")

# We get the sample dataset
df = sns.load_dataset("iris")

# We make a histogram but split by categories
fig = sns.displot(
    df, x="sepal_length", col="species",
    binwidth=1, height=3,
)
How to plot a histogram but split by categories using Seaborn

Using the seaborn.displot() function we will plot a histogram by default. We can specify which variable we want to analyze with the x parameter.

Here we will analyze the sepal_length variable and split it by column which will be species in our case.

The result

Here is how to plot a histogram but split by category

Here you are! You now know how to plot a histogram but split it by categories using Seaborn in Python!

More on seaborn

If you want to know more about how to use Seaborn, etc... check out the other articles I wrote on the topic, just here :

Seaborn - The Python You Need
We gathered the only Python essentials that you will probably ever need.