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.
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:
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)
Contact Us