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.
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.
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)