Plot Multiple Histograms On Same Plot With Seaborn

Histograms are a powerful tool for visualizing the distribution of data in a dataset. When working with multiple datasets or variables, it can be insightful to compare their distributions side by side. Seaborn, a python data visualization package offers powerful tools for making visually appealing maps and efficient way to plot multiple histograms on the same plot.

In this article, we will explore and implement multiple histograms on same plot.

Table of Content

  • Understanding Overlaying Histograms using Seaborn
  • Comparing Two Distributions : A Practical Example
  • Overlaying Histograms with Kernel Density Estimation (KDE)

Understanding Overlaying Histograms using Seaborn

Plotting multiple Histograms On Same Plot is built by splitting the data into periods, or “bins,” and then showing the number or density of readings within each bin as bars. Unlike different histograms, where each group has its own plot, multiple histograms show multiple distributions within the same plot, making it easier to compare them clearly.

Why Use Seaborn for Overlapping Histograms?

Seaborn offers a streamlined approach to creating aesthetically pleasing and informative histograms with simplified lines of code:

  • Simplified Syntax: Concise code for generating complex plots.
  • Statistical Integration: Built-in functions for density estimation (KDE).
  • Customization: Extensive options for tailoring the appearance.

Comparing Two Distributions : A Practical Example

This code imports Seaborn and Matplotlib, loads iris dataset, extracts petal lengths for setosa and versicolor species, plots overlapping histograms for these species’ petal lengths, and adds labels and legend to the plot before displaying it.

Python
import seaborn as sns
import matplotlib.pyplot as plt

# Sample data
data1 = sns.load_dataset('iris').query("species == 'setosa'")['petal_length']
data2 = sns.load_dataset('iris').query("species == 'versicolor'")['petal_length']

# Plotting overlapping histograms
sns.histplot(data=data1, color='blue', alpha=0.5, label='Setosa')
sns.histplot(data=data2, color='orange', alpha=0.5, label='Versicolor')

# Adding labels and legend
plt.xlabel('Petal Length')
plt.ylabel('Frequency')
plt.legend()

plt.show()

Output:

Customizing Overlapping Histograms

Seaborn provides flexibility for tailoring histograms:

  • Kernel Density Estimation (KDE): Smooth the histogram with a density curve.
  • Colors and Transparency: Adjust to enhance visual clarity.
  • Multiple Datasets: Easily add more distributions to the plot.
Python
import seaborn as sns
import matplotlib.pyplot as plt

# Sample data
data1 = sns.load_dataset('titanic').query("`class` == 'First'")['age'].dropna()
data2 = sns.load_dataset('titanic').query("`class` == 'Third'")['age'].dropna()

# Plotting overlapping histograms with KDE
sns.histplot(data=data1, color='blue', alpha=0.5, kde=True, label='First Class')
sns.histplot(data=data2, color='orange', alpha=0.5, kde=True, label='Third Class')

# Adding labels and legend
plt.xlabel('Age')
plt.ylabel('Density')
plt.legend()

plt.show()

Output:

Overlaying Histograms with Kernel Density Estimation (KDE)

This code imports Seaborn and Matplotlib, loads Titanic dataset, extracts ages of passengers in the first and third class, plots overlapping histograms with kernel density estimation for age distribution in each class, adds labels for age and density, and displays the plot with a legend.

Python
import seaborn as sns
import matplotlib.pyplot as plt

# Sample data
data1 = sns.load_dataset('titanic').query("class == 'First'")['age'].dropna()
data2 = sns.load_dataset('titanic').query("class == 'Third'")['age'].dropna()

# Plotting overlapping histograms with KDE
sns.histplot(data=data1, color='blue', alpha=0.5, kde=True, label='First Class')
sns.histplot(data=data2, color='orange', alpha=0.5, kde=True, label='Third Class')

# Adding labels and legend
plt.xlabel('Age')
plt.ylabel('Density')
plt.legend()

plt.show()

Output:

Overlaying histogram with Kde on same Plot

Conclusion

By leveraging Seaborn’s intuitive syntax and customization options, you can generate informative plots that reveal hidden patterns and relationships in your data. Whether you’re exploring simple comparisons or delving into complex analyses.

Key Takeways:

  • Overlapping histograms are ideal for comparing data distributions.
  • Seaborn simplifies the creation and customization of these plots.
  • Experiment with different datasets and parameters to unlock insights.


Contact Us