How to plot a histogram but split it by categories using Seaborn in Python
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
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,
)
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 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 :
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚